• 阅读{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是怎么一回事。
    对于阅读源代码这种事情,根据每个人能力不同理解也不同,感悟也不同,所以还请大神轻喷。
    如果哪里有问题请指出来,互相学习,谢谢。

  • 相关阅读:
    日记10硬件与操作系统安装专用
    程序设计与算法(三)C++面向对象程序设计 (北大MOOC)
    macbook pro14 前端基本配置【20211114】
    springboot配置rabbitmq的序列化反序列化格式
    sql优化把派生表改成子查询,查询速度将变快
    Android开发 因为ViewPager与SwipeRefreshLayout冲突导致RecyclerView或者其他列表布局的item无法点击的问题
    Kotlin开发 委托
    Kotlin开发 协程的实践 Retrofit + 协程 + ViewModel
    git clean用法
    kotlin开发 高阶函数学习与记录
  • 原文地址:https://www.cnblogs.com/symons1992/p/4425500.html
Copyright © 2020-2023  润新知