视图家族
Posted cherish937426
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视图家族相关的知识,希望对你有一定的参考价值。
GenericAPIView基类
# GenericAPIView是继承APIView的,使用完全兼容APIView # 重点:GenericAPIView在APIView基础上完成了哪些事 # 1)get_queryset():从类属性queryset中获得model的queryset数据 # 2)get_object():从类属性queryset中获得model的queryset数据,再通过有名分组pk确定唯一操作对象 # 3)get_serializer():从类属性serializer_class中获得serializer的序列化类 urlpatterns = [ url(r‘^v2/books/$‘, views.BookGenericAPIView.as_view()), url(r‘^v2/books/(?P<pk>.*)/$‘, views.BookGenericAPIView.as_view()), ] from rest_framework.generics import GenericAPIView class BookGenericAPIView(GenericAPIView): queryset = models.Book.objects.filter(is_delete=False) serializer_class = serializers.BookModelSerializer # 自定义主键的 有名分组 名 lookup_field = ‘pk‘ # 群取 # def get(self, request, *args, **kwargs): # book_query = self.get_queryset() # book_ser = self.get_serializer(book_query, many=True) # book_data = book_ser.data # return APIResponse(results=book_data) ? # 单取 def get(self, request, *args, **kwargs): book_query = self.get_object() book_ser = self.get_serializer(book_query) book_data = book_ser.data return APIResponse(results=book_data)
mixins视图工具集 - 辅助GenericAPIView
# 1)mixins有五个工具类文件,一共提供了五个工具类,六个工具方法:单查、群查、单增、单删、单整体改、单局部改 # 2)继承工具类可以简化请求函数的实现体,但是必须继承GenericAPIView,需要GenericAPIView类提供的几个类属性和方法(见上方GenericAPIView基类知识点) # 3)工具类的工具方法返回值都是Response类型对象,如果要格式化数据格式再返回给前台,可以通过 response.data 拿到工具方法返回的Response类型对象的响应数据 urlpatterns = [ url(r‘^v3/books/$‘, views.BookMixinGenericAPIView.as_view()), url(r‘^v3/books/(?P<pk>.*)/$‘, views.BookMixinGenericAPIView.as_view()), ] from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin class BookMixinGenericAPIView(ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericAPIView): queryset = models.Book.objects.filter(is_delete=False) serializer_class = serializers.BookModelSerializer ? def get(self, request, *args, **kwargs): if ‘pk‘ in kwargs: response = self.retrieve(request, *args, **kwargs) else: # mixins提供的list方法的响应对象是Response,想将该对象格式化为APIResponse response = self.list(request, *args, **kwargs) # response的数据都存放在response.data中 return APIResponse(results=response.data) ? def post(self, request, *args, **kwargs): response = self.create(request, *args, **kwargs) return APIResponse(results=response.data) ? def put(self, request, *args, **kwargs): response = self.update(request, *args, **kwargs) return APIResponse(results=response.data) ? def patch(self, request, *args, **kwargs): response = self.partial_update(request, *args, **kwargs) return APIResponse(results=response.data)
工具视图
# 1)工具视图都是GenericAPIView的子类,且不同的子类继承了不听的工具类,重写了请求方法 # 2)工具视图的功能如果直接可以满足需求,只需要继承工具视图,提供queryset与serializer_class即可 urlpatterns = [ url(r‘^v4/books/$‘, views.BookListCreatePIView.as_view()), url(r‘^v4/books/(?P<pk>.*)/$‘, views.BookListCreatePIView.as_view()), ] from rest_framework.generics import ListCreateAPIView, UpdateAPIView class BookListCreatePIView(ListCreateAPIView, UpdateAPIView): queryset = models.Book.objects.filter(is_delete=False) serializer_class = serializers.BookModelSerializer
视图集
# 1)视图集都是优先继承ViewSetMixin类,再继承一个视图类(GenericAPIView或APIView) # GenericViewSet、ViewSet # 2)ViewSetMixin提供了重写的as_view()方法,继承视图集的视图类,配置路由时调用as_view()必须传入 请求名-函数名 映射关系字典 # eg: url(r‘^v5/books/$‘, views.BookGenericViewSet.as_view({‘get‘: ‘my_get_list‘})), # 表示get请求会交给my_get_list视图函数处理 urlpatterns = [ # View的as_view():将get请求映射到视图类的get方法 # ViewSet的as_view({‘get‘: ‘my_get_list‘}):将get请求映射到视图类的my_get_list方法 url(r‘^v5/books/$‘, views.BookGenericViewSet.as_view({‘get‘: ‘my_get_list‘})), url(r‘^v5/books/(?P<pk>.*)/$‘, views.BookGenericViewSet.as_view({‘get‘: ‘my_get_obj‘})), ] from rest_framework.viewsets import GenericViewSet from rest_framework import mixins class BookGenericViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, GenericViewSet): queryset = models.Book.objects.filter(is_delete=False) serializer_class = serializers.BookModelSerializer ? def my_get_list(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) ? def my_get_obj(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs)
GenericAPIView 与 APIView 最为两大继承视图的区别
# 1)GenericViewSet和ViewSet都继承了ViewSetMixin,as_view都可以配置 请求-函数 映射 # 2)GenericViewSet继承的是GenericAPIView视图类,用来完成标准的 model 类操作接口 # 3)ViewSet继承的是APIView视图类,用来完成不需要 model 类参与,或是非标准的 model 类操作接口 # post请求在标准的 model 类操作下就是新增接口,登陆的post不满足 # post请求验证码的接口,不需要 model 类的参与 # 案例:登陆的post请求,并不是完成数据的新增,只是用post提交数据,得到的结果也不是登陆的用户信息,而是登陆的认证信息
工具视图集
urlpatterns = [ url(r‘^v6/books/$‘, views.BookModelViewSet.as_view({‘get‘: ‘list‘, ‘post‘: ‘create‘})), url(r‘^v6/books/(?P<pk>.*)/$‘, views.BookModelViewSet.as_view({‘get‘: ‘retrieve‘, ‘put‘: ‘update‘, ‘patch‘: ‘partial_update‘, ‘delete‘: ‘destroy‘})), ] from rest_framework.viewsets import ModelViewSet class BookModelViewSet(ModelViewSet): queryset = models.Book.objects.filter(is_delete=False) serializer_class = serializers.BookModelSerializer ? # 删不是数据库,而是该记录中的删除字段 def destroy(self, request, *args, **kwargs): instance = self.get_object() # type: models.Book if not instance: return APIResponse(1, ‘删除失败‘) # 实际操作,在此之前就做了判断 instance.is_delete = True instance.save() return APIResponse(0, ‘删除成功‘)
路由组件(了解)
from django.conf.urls import include
from rest_framework.routers import SimpleRouter router = SimpleRouter() # 所有路由与ViewSet视图类的都可以注册,会产生 ‘^v6/books/$‘ 和 ‘^v6/books/(?P<pk>[^/.]+)/$‘ router.register(‘v6/books‘, views.BookModelViewSet) ? urlpatterns = [ # 第一种添加子列表方式 url(r‘^‘, include(router.urls)), ] # 第二种添加子列表方式 # urlpatterns.extend(router.urls)
以上是关于视图家族的主要内容,如果未能解决你的问题,请参考以下文章