视图类五层
第一层:继承APIview,五个接口
第二层:GenericAPIView,继承APIview(两个类属性+def get post get put delete)
第三层:五个视图扩展类 GenericAPIView +(RetrieveModelMixin, CreateModelMixin, DestroyModelMixin,ListModelMixin,UpdateModelMixin)(两个类属性+def get post get put delete)
第四层:9个视图子类(ListAPIView, CreateAPIView, ListCreateAPIView,RetrieveAPIView, DestroyAPIView, UpdateAPIView,RetrieveUpdateDestroyAPIView, RetrieveDestroyAPIView, RetrieveUpdateAPIView) (两个类属性)
第五层视图集
9个视图子类
from rest_framework.generics import ListAPIView, CreateAPIView, ListCreateAPIView
from rest_framework.generics import RetrieveAPIView, DestroyAPIView, UpdateAPIView
from rest_framework.generics import RetrieveUpdateDestroyAPIView, RetrieveDestroyAPIView, RetrieveUpdateAPIView
想写五个接口中的某一个或某几个或者所有,只需要选择继承不同的类即可
需要配置下列两个属性:
queryset = 表名.objects.all()
serializer_class = 序列化类
from rest_framework.generics import ListCreateAPIView,CreateAPIView,ListAPIView from rest_framework.generics import UpdateAPIView,DestroyAPIView,RetrieveAPIView,RetrieveUpdateDestroyAPIView,RetrieveUpdateAPIView,RetrieveDestroyAPIView class PublishView(ListCreateAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializer class PublishDetialView(RetrieveUpdateDestroyAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializer
视图集
ModelViewSet:视图类继承它,需要改路由,5个接口都有路由配置:
视图:
源码分析:
ViewSetMixin里的as_view
以后视图类中方法名可以随意命名,只要路由做好映射:
例如:在视图类中写一个login方法,然后路由匹配成如下所示,就可以执行login
继承的类是:ViewSetMixin
视图类中方法任意命名,路由根据其一起变化。
ReadOnlyModelViewSet
以后写的接口,只想有 获取单条和获取所有,继承它
标签:子类,rest,framework,视图,generics,import,class From: https://www.cnblogs.com/YeeQX/p/17680321.html