rest-framework视图
一、关于视图的认识
- 1、
django
中使用类来写视图的时候我们都要继承一个View
的类 - 2、前面我们使用
rest-framework
的时候都要继承一个APIView
的类
二、关于GenericAPIView
的使用
1、定义
url
from django.urls import path from . views import role, user, group urlpatterns = [ ... path('role4/', role.RoleView4.as_view()) ]
2、定义视图类
from rest_framework.generics import GenericAPIView class RoleView4(GenericAPIView): # 获取全部的数据 queryset = models.RoleModel.objects.all() # 序列化的类 serializer_class = RoleSerializer3 # 分页的类 pagination_class = MyPageNumberPagination def get(self, request, *args, **kwargs): roles = self.get_queryset() pager_roles = self.paginate_queryset(roles) ser = self.get_serializer(instance=pager_roles, many=True) return Response(ser.data)
3、总结
- 1.这个类继承了
APIview
- 2.不太好用,先定义了类还要定义get的方法
- 1.这个类继承了
三、关于GenericViewSet
的使用
1、定义视图类
from rest_framework.viewsets import GenericViewSet class RoleView5(GenericViewSet): # 获取全部的数据 queryset = models.RoleModel.objects.all() # 序列化的类 serializer_class = RoleSerializer3 # 分页的类 pagination_class = MyPageNumberPagination def list(self, request, *args, **kwargs): roles = self.get_queryset() pager_roles = self.paginate_queryset(roles) ser = self.get_serializer(instance=pager_roles, many=True) return Response(ser.data)
2、定义
url
urlpatterns = [ ... path('role5/', role.RoleView5.as_view({'get': 'list', })) ]
3、总结
- 1.
GenericViewSet
类继承了ViewSetMixin, generics.GenericAPIView
, - 2.其中
ViewSetMixin
类就是重写了as_view()
方法就是 - 3.这个视图也不太好用
- 1.
四、使用ListModelMixin
和GenericViewSet
类
1、视图类
from rest_framework.mixins import ListModelMixin class RoleView6(ListModelMixin, GenericViewSet): queryset = models.RoleModel.objects.all() serializer_class = RoleSerializer3 pagination_class = MyPageNumberPagination
2、定义
url
urlpatterns = [ ... path('role6/', role.RoleView6.as_view({'get': 'list', })) ]
3、关于
ListModelMixin
的源码(会帮我们实现上面写的GenericViewSet
中代码)class ListModelMixin(object): """ List a queryset. """ 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)
五、关于使用CreateModelMixin
创建数据
1、视图类
from rest_framework.mixins import ListModelMixin, CreateModelMixin class RoleView6(ListModelMixin, CreateModelMixin, GenericViewSet): queryset = models.RoleModel.objects.all() serializer_class = RoleSerializer3 pagination_class = MyPageNumberPagination
2、关于
url
配置urlpatterns = [ path('role6/', role.RoleView6.as_view({'get': 'list', 'post': 'create'})) ]
六、终极版直接继承ModelViewSet
1、
ModelViewSet
类源码(什么都没做,只是多继承了几个)class ModelViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, GenericViewSet): """ A viewset that provides default `create()`, `retrieve()`, `update()`, `partial_update()`, `destroy()` and `list()` actions. """ pass
2、关于上面继承几个类的使用
- 1.
CreateModelMixin
创建数据/新增数据 - 2.
RetrieveModelMixin
获取单条数据 - 3.
UpdateModelMixin
更新数据(分全部与局部更新) - 4.
DestroyModelMixin
删除数据 - 5.
ListModelMixin
获取列表数据
- 1.
3、视图类的代码
from rest_framework.viewsets import ModelViewSet class RoleView6(ModelViewSet): queryset = models.RoleModel.objects.all() serializer_class = RoleSerializer3 pagination_class = MyPageNumberPagination
4、重点是配置
url
可以查看源码进行配置
urlpatterns = [ ... path('role5/', role.RoleView5.as_view({'get': 'list', })), path('role6/', role.RoleView6.as_view({'get': 'list', 'post': 'create'})), path('role6/<int:pk>/', role.RoleView6.as_view({'get': 'retrieve', 'delete': 'destroy', 'put': 'update', 'patch': 'partial_update'})), ]
5、关于
put
和patch
的认识- 1.
put
是全局更新 - 2.
patch
是局部更新
- 1.
七、总结:关于使用哪个视图类来继承的问题
1、如果涉及到增删改查就直接使用
ModelViewSet
from rest_framework.viewsets import ModelViewSet class GoodView(ModelViewSet): pass
2、如果是增删就可以单独继承
CreateModelMixin
、DestroyModelMixin
、GenericViewSet
from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import CreateModelMixin, DestroyModelMixin class GoodView(CreateModelMixin, DestroyModelMixin, GenericViewSet): pass
3、如果业务逻辑比较复杂还是建议使用
APIView
或者GenericViewSet
- 4、注意要合理的配置路由
八、重点:路由的注册
使用场景:上面介绍的,使用了混入及
genericViewSet
的时候需要特别配置url
,配置步骤如下:
1、定义好
serializer
from rest_framework import serializers from goods import models class GoodsSerializer(serializers.ModelSerializer): """ 序列化商品 """ class Meta(object): model = models.GoodsModel fields = '__all__'
2、视图类
from rest_framework.viewsets import GenericViewSet, ModelViewSet from rest_framework.mixins import ListModelMixin from goods import serializers from goods import models class GoodView(ListModelMixin, GenericViewSet): """ 配置产品的视图类 """ queryset = models.GoodsModel.objects.all() serializer_class = serializers.GoodsSerializer
3、路由的注册
from django.urls import path, include, re_path from rest_framework.routers import DefaultRouter from goods.views.goods import GoodView router = DefaultRouter() router.register('goods', GoodView, base_name='goods') router.register('category', goods.GoodsCategoryView, base_name='category') ... urlpatterns = [ path('', include(router.urls)), ]