zoukankan      html  css  js  c++  java
  • 阅读{django-restframework}源码[generics.py]学习笔记

    首先django-restframework是基于django的一个框架。
     
    mixins.py中开头是这样写的:
    Basic building blocks for generic class based views.
    We don't bind bahaviour to http method handlers yet, whick allows mixin classes to be composed in interesting ways.
     
    generics是把对数据库的增删改查都放在一起了。
     
    对数据的查找是调用了django框架里面的函数。
     
    ===============================================================
    generics.py总体结构
     
    """
    Generic views that provide commonly needed behaviour.
    """
     
    import ....
     
    def get_object_or_404
    class GenericAPIView
         """
         Base class for all other generic views.     增删改查的基类,要注意下面增删改查的还有一个基类就是mixins.*
         """
         def get_queryset
         def get_objects
         def get_serializer
         def get_serializer_class
         def get_serializer_context
         def filter_queryset
         def paginator
         def paginate_queryset
         def get_paginated_response
     
    class CreateAPIView     增
    class ListAPIView     查
    class RetrieveAPIView     查       
    class DestroyAPIView     删
    class UpdateAPIView     改
    class ListCreateAPIView     查     增
    class RetrieveUpdateAPIView     查     增
    class RetrieveDestroyAPIView     查     删
    class RetrieveUpdateDestroyAPIView     查     改     删
     
    =====pk的问题==============================
    关于pk的问题,默认是pk,如果用了pk,直接就可以过滤也不用去获取这个参数,那不用pk为什么就要去获取参数呢?不用pk也不获取行不行呢? 让我们来看
    class GenericAPIView(Views.APIView):
         lookup_field = 'pk'
         def get_object(self):
              lookup_url_kwarg = self.look_url_kwarg or self.lookup_field
              filter_kwargs = { self.lookup_field: self.kwargs[lookup_url_kwarg]}
              obj = get_object_or_404(queryset, **filter_kwargs)
              return obj
    答案是不行!
    由此我们可以看出来pk是写死在代码里面的,至于为什么是pk,不是rk,可能是源代码开发人员喜欢玩传奇。。。调侃一下,不要在意
     
    而且我们也能看出来,在url里面只能有一个参数。不可以加多个参数。这个问题也要注意。
     
    注:这上面的代码不是全部代码,我只是把与pk有关的提取出来写出来。
     
    =================================================================
    我在mixins.py里面看到一个函数调用:
    class RetrieveModelMixin(object):
         """
         Retrieve a model instance.
         """
         def retrieve(self, request, *args, **kwargs):
              instance = self.get_object()
              serializer = self.get_serializer(instance)
              return Response(serializer.data)
    我们可以看到这段代码用了get_object()可是我在mixins.py里面没有看到这个函数定义,然后发现,原来是是在generics.py里面的class GenericAPIView(views.APIView)里面定义了这个函数,还没完,然后紧接着可以看到class RetrieveAPIView(mixins.RetrieveModelMixin, GenericAPIView)
    ===============================================================
     
    这样就可以解释清楚了,这样就可以大致理解了django-restframework中generics.py是怎么一回事。
    对于阅读源代码这种事情,根据每个人能力不同理解也不同,感悟也不同,所以还请大神轻喷。
    如果哪里有问题请指出来,互相学习,谢谢。

  • 相关阅读:
    Squid-Squid 多层Cache 如何设置实现墙内直连,墙外域名走国外Proxy
    利用win2008虚拟化hyper-v 和squid反向代理,自己做个IDC
    再次分享 pyspider 爬虫框架
    刘宇:我如何5分钟拿到李书福的投资?
    刘宇:2014年投资感悟
    刘宇(正和磁系资本创始人)_百度百科
    python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客
    采集爬虫中,解决网站限制IP的问题?
    Web 应用性能和压力测试工具 Gor
    dnspod-sr内网轻量级DNS首选方案
  • 原文地址:https://www.cnblogs.com/symons1992/p/4425500.html
Copyright © 2011-2022 走看看