从miminx 类开始,依次有子类RetrieveModelMixin(单个get 请求))
,ListModelMixin(LIST请求),CreateModelMixin(POST请求),UpdateModelMixin(update更新请求),DestroyModelMixin(delete)删除请求
从url角度总结:
url(r'^(?P<version>[v1|v2]+)/test1/$', views.TestView1.as_view({'get':'list','post':'create'})), url(r'^(?P<version>[v1|v2]+)/test1/(?P<pk>+d)$', views.TestView1.as_view({'put':'update','patch':'partial_update', 'delete':'destroy','get':'retrieve'}))
源码分析:以ListAPIView分析
from rest_framework import response from rest_framework.generics import ListAPIView,RetrieveAPIView from rest_framework.response import Response from auction.utils.PageFilter.filters import PullDownRefreshFilter,ReachBottomFilter from auction.utils.PageFilter.paginations import AuctionLimitPagination from api import models from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import RetrieveModelMixin,ListModelMixin class auctionView(ListAPIView): # 如果是列表必须两个数据,一个是查询数据,一个是序列化 queryset = models.Auction.objects.filter(status__gt=0).order_by('id') serializer_class = auctionSerializers filter_backends = [ReachBottomFilter,PullDownRefreshFilter] pagination_class = AuctionLimitPagination 源码解析: # 第一步:LISTAPIView 继承mixins.ListModelMixin,GenericAPIView;ListModelMixin类中def get()方法,可以替视图接受GET请求 class ListAPIView(mixins.ListModelMixin, GenericAPIView): """ Concrete view for listing a queryset. """ def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) # 第二步 执行类ListModelMixin的self.list()方法 def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) #第三步执行self.get_querset()获取queryset ,是类GenericAPIView的方法 def get_queryset(self): assert self.queryset is not None, ( "'%s' should either include a `queryset` attribute, " "or override the `get_queryset()` method." % self.__class__.__name__ ) queryset = self.queryset# 获取 ListAPIView的queryset # 是否是对象,类,或元组, if isinstance(queryset, QuerySet): # Ensure queryset is re-evaluated on each request. queryset = queryset.all() return queryset # 第四步 执行self.filter_queryset(queryset)是类GenericAPIView的方法 # 过滤queryset def filter_queryset(self, queryset): "" """ for backend in list(self.filter_backends):#self.filter_backends过滤条件 queryset = backend().filter_queryset(self.request, queryset, self) return queryset # 第五步 执行self.paginate_querys(),是类GenericAPIView的方法 # 对queryset进行分页处理 def paginate_queryset(self, queryset): """ Return a single page of results, or `None` if pagination is disabled. """ if self.paginator is None: return None return self.paginator.paginate_queryset(queryset, self.request, view=self) # 第六步 执行self.get_serializer方法(),返回self.serializer_class,并实例序列化 def get_serializer_class(self): assert self.serializer_class is not None, ( "'%s' should either include a `serializer_class` attribute, " "or override the `get_serializer_class()` method." % self.__class__.__name__ ) return self.serializer_class # 第七步 get_paginated_response(serializer.data) ,返回序列化结果
其他几个类
RetrieveModelMixin(单个get 请求)),CreateModelMixin(POST请求),UpdateModelMixin(update更新请求),DestroyModelMixin(delete)删除请求源码分析同理。