-
两个视图基类
-
五个视图扩展类
两个视图基类
# 之前写5个接口,继承APIView
# 还可以继承GenericAPIView:它帮我们干了好多事
# GenericAPIView:属性和方法
-属性
-queryset # 要序列化的数据
-serializer_class # 序列化类
-----了解------
-lookup_field # 通过get_object 获取单个对象的查询key值,value值是路由中传进来的
-filter_backends # 过滤类
-pagination_class # 分页类
-方法
-get_queryset # 获取要序列化的数据
-get_object # 根据lookup_field配置的参数获取单个对象
-get_serializer # 获取序列化类,咱们直接用的
-get_serializer_class # 获取序列化类,不是咱们直接用的get_serializer调用了它
-------了解-----
-filter_queryset # 跟过滤有关系
-paginate_xxx # 跟分页有关
五个视图扩展类
必须配合GenericAPIView使用,不能配合APIView使用
# RetrieveModelMixin:写了一个Create方法,就是原来咱们post中的代码
# RetrieveModelMixin:retrieve,就是咱们原来的get
# UpdateModelMixin:update方法,就是咱们原来的put
# ListModelMixin:list方法,就是原来咱们的get
# DestroyModelMixin:destroy方法,就是原来咱们的delete
基于GenericAPIView+5个视图扩展类编写5个接口
views.py
from .models import Publish
from .serializer import PublishModelSerializer
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin, \
ListModelMixin
class PublishView(GenericAPIView, ListModelMixin, CreateModelMixin):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class PublishDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def Put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
9个视图子类
# 视图类:继承了GenericAPIView+某个或某几个视图扩展类
基于9个视图子类编写5个接口
views.py
from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveAPIView, DestroyAPIView, UpdateAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveDestroyAPIView, \
RetrieveUpdateAPIView
class PublishView(ListCreateAPIView):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
class PublishDetailView(RetrieveUpdateDestroyAPIView):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
视图集
# 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中:
# list() 提供一组数据
# retrieve() 提供单个数据
# create() 创建数据
# update() 保存数据
# destory() 删除数据
# ViewSet视图集类不再实现get()、post()等方法,而是实现动作 action 如 list() 、create() 等。
继承ModelViewSet编写5个接口
urls.py
from django.contrib import admin
from django.urls import path
from app01.views import PublishView
urlpatterns = [
path('admin/', admin.site.urls),
path('publish/', PublishView.as_view({'get': 'list', 'post': 'create'})),
path('publish/<int:pk>/', PublishView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}))
]
views.py
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
class PublishView(ModelViewSet):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
ViewSetMixin源码分析
from rest_framework.viewsets import ViewSetMixin, ViewSet, GenericViewSet
# ViewSet: APIView+ViewSetMixin的组合
# ViewSetMixin: 控制了路由的写法
# GenericViewSet: GenericAPIView+ViewSetMixin的组
"""
ModelViewSet它继承了很多类,其中GenericViewSet继承了GenericAPIView+ViewSetMixin,而ViewSetMixin方法之所以能够控制路由,就是因为它重写了as_view
所以在路由配置中的as_view,其实已经不是APIview中的as_view了,这里的as_view就是ViewSetMixin方法的as_view
继承ViewSetMixin的as_view,是根据继承的查找顺序执行的 userview>>>>ModelViewSet>>>>GenericAPIView>>>>>ViewSetMixin
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet
"""
class ViewSetMixin:
@classonlymethod
def as_view(cls, actions=None, **initkwargs):
# 路由中as_view中必须传参数,必须传字典:{'get': 'list', 'post': 'create'}
if not actions:
raise TypeError("The `actions` argument must be provided when "
"calling `.as_view()` on a ViewSet. For example "
"`.as_view({'get': 'list'})`")
# 路由匹配成功,执行view(request),request是老的request
def view(request, *args, **kwargs):
# actions={'get': 'list', 'post': 'create'}
for method, action in actions.items():
# method:get action:list
# self 是视图类的对象中通过反射,查找list,
# handler视图类中的list方法
handler = getattr(self, action)
# 向视图类的对象中,反射 method:get,handler:list方法
# self.get=list
setattr(self, method, handler)
return self.dispatch(request, *args, **kwargs)
return csrf_exempt(view)
# 只要继承了ViewSetMixin,路由写法就会变,都要写成:views.UserView.as_view({'get': 'list', 'post': 'create'}))
# 这样写好以后,对应的请求方式来了,就会执行配置的方法
# 扩展:
-以后只要继承了ViewSetMixin,视图类中可以写任意名字的方法,不用非得写get,post,delete
标签:get,list,self,request,视图,五个,基类,view From: https://www.cnblogs.com/scx-xiaochun/p/16759102.html