视图组件

视图家族

views:视图类:

-APIView,

-GenericAPIView

mixin:视图工具类:

-CreateModelMixin,

-ListModelMixin,

-RetrieveModelMixin,

-UpdateModelMixin,其中包含两个工具:update,partial_update

-DestroyModelMixin,

generics:工具视图类:

九个

viewsets:视图集:

-两个视图集基类

-两个视图集子类

-一个工具类

视图基类

APIView

from rest_framework.views import APIView

  • 继承自View父类,支持View的所有方法

  • 重写了as_view方法,禁用了csrf中间件

  • 重写了dispatch方法,在dispatch中完成了五大模块以及三大认证等方法

  • 加入了一系列类属性

    • authentication_classes 列表或元组,身份认证类
    • permission_classer 列表或元组,权限检查类
    • throttle_classes 列表或元组,流量控制类

GenericAPIView类[通用视图类]

from rest_framework.generics import GenericAPIView

  1. 继承自APIView

  2. get_queryset方法,配置queryset类属性,提供视图类相关的Models

  3. 在2的基础上,get_object方法,配置lookup_url_kwargs类属性,提供视图类相关的具体Model

  4. get_serializer方法,配置serializer_class类属性,提供视图类相关的序列化对象

总结:

GenericAPIView就是在APIView基础上提供了三个方法,三个类属性,需要配合视图工具类来体现优势.

视图工具类mixins

五个类,六个方法

CreateModelMixin

完成单增

def create(self, request, *args, **kwargs):
ListModelMixin

完成群查

def list(self, request, *args, **kwargs):
RetrieveModelMixin

完成单查

def retrieve(self, request, *args, **kwargs):
UpdateModelMixin

完成整体单改

def update(self, request, *args, **kwargs):

完成局部单改

def perform_update(self, serializer):
DestroyModelMixin

完成单删:

def destroy(self, request, *args, **kwargs):

注意:这里的删除是删除表记录,生产中,常设置一字段来标识此记录是否删除,所以实际生产中自己重写destroy方法.

注意

因为这六个方法的实现体,调用的方法都是GenericAPIView类提供的,所以需要配合GenericAPIView类使用

工具视图类generics

九种组合

  1. 通过不同种类的mixins与GenericAPIView组合来完成需要的功能

  2. 不同的组合实现对应的get,post,put,patch,delete方法

  3. 需要自己配置三个类属性

    queryset、serializer_class、lookup_url_kwarg

视图集viewsets

视图集都继承了ViewSetMixin类,该类重写了as_view方法,与APIView对比,ViewSetMixin多出了一个actions参数,内部通过

for method, action in actions.items():
    handler = getattr(self, action)    
    setattr(self, method, handler)

getattr,与setattr方法将url中字典中的key与后台函数(大概率是视图工具中的函数)的函数名进行一一映射,

  1. 两个视图集基类ViewSet和GenericViewSet

    1. GenericViewSet(ViewSetMixin, GenericAPIView),该分支控制访问与数据库具体操作的接口.
    2. ViewSet(ViewSetMixin, APIView):该分支满足与model类关系不大的接口,比如登录接口,短信验证码接口
  2. 三个视图类

    只是继承了一堆方法,流氓类

    1. ModelViewSet方法

      class ModelViewSet(mixins.CreateModelMixin,
                         mixins.RetrieveModelMixin,
                         mixins.UpdateModelMixin,
                         mixins.DestroyModelMixin,
                         mixins.ListModelMixin,
                         GenericViewSet):
          pass
      
    2. ReadOnlyModelViewSet方法

      class ReadOnlyModelViewSet(mixins.RetrieveModelMixin,
                                 mixins.ListModelMixin,
                                 GenericViewSet):
          pass
      
    3. ViewSet方法

      class ViewSet(ViewSetMixin, views.APIView):
          pass
      

路由层

  1. 导入模块

    from rest_framework.routers ``import SimpleRouter

  2. 初始化路由对象

    router ``= SimpleRouter()

  3. 创建路由

    # 注册各种接口路由
    router.register('cars', views.CarModelViewSet, base_name='car')
    #car为链接的开头,views.CarModelViewSet为当路由为cars的时候的调用的函数
    urlpatterns =[]
    urlpatterns.extend(router.urls)
    

    其想过等价于

    urlpatterns = [
        url(r'^cars/$', views.CarModelViewSet.as_view()),
        url(r'^cars/(?P<pk>.*)/$', views.CarModelViewSet.as_view()),
    ]
    
原文地址:https://www.cnblogs.com/agsol/p/12121914.html