视图
继承关系,视图可以优化api接口代码逻辑
举例:
表:
1 from django.db import models 2 3 4 class UserGroup(models.Model): 5 title = models.CharField(max_length=32) 6 7 8 class UserInfo(models.Model): 9 user_type_choices = ( 10 (1, '普通用户'), 11 (2, 'vip'), 12 (3, 'svip'), 13 ) 14 user_type = models.IntegerField(choices=user_type_choices) 15 username = models.CharField(max_length=32, unique=True) 16 password = models.CharField(max_length=64) 17 group = models.ForeignKey('UserGroup', on_delete=models.CASCADE) 18 roles = models.ManyToManyField('Role') 19 20 21 class UserToken(models.Model): 22 user = models.OneToOneField(to='UserInfo', on_delete=models.CASCADE) 23 token = models.CharField(max_length=64) 24 25 26 class Role(models.Model): 27 title = models.CharField(max_length=32)
路由:
1 from django.contrib import admin 2 from django.urls import path, re_path, include 3 4 urlpatterns = [ 5 path('admin/', admin.site.urls), 6 re_path('api/', include('api.urls')), 7 ]
分发的路由:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 from django.urls import path, re_path, include 5 from api import views 6 from rest_framework import routers 7 8 router = routers.DefaultRouter() 9 router.register('xx', views.View3View) 10 11 urlpatterns = [ 12 # 视图 13 re_path('(?P<version>[v1|v2]+)/view1/$', views.View1View.as_view()), 14 # 视图裂开了 15 re_path('(?P<version>[v1|v2]+)/view2/$', views.View2View.as_view({'get': 'list'})), 16 # 路由 http://127.0.0.1:8000/api/v1/view3/?format=json&page=2 17 re_path('(?P<version>[v1|v2]+)/view3/$', views.View3View.as_view({'get': 'list', 'post': 'create'})), 18 # 路由 http://127.0.0.1:8000/api/v1/view3.json 19 re_path('(?P<version>[v1|v2]+)/view3.(?P<format>w+)$', views.View3View.as_view({'get': 'list', 'post': 'create'})), 20 # 对数据操作获取到id 21 re_path('(?P<version>[v1|v2]+)/view4/(?P<pk>d+)/$', views.View4View.as_view({'get': 'retrieve', 'delete': 'destroy', 'put': 'update', 'patch': 'partial_update'})), 22 23 # 路由自动生成url 24 re_path('(?P<version>[v1|v2]+)/', include(router.urls)), 25 ]
视图 (ps:序列化用的上一篇的序列化类,分页类用的内置的)
1 # ----------------------------视图继承GenericAPIView----------------------------- 2 from rest_framework.generics import GenericAPIView 3 4 5 class View1View(GenericAPIView): 6 """ 7 不太常用感觉 8 """ 9 queryset = models.Role.objects.all() 10 serializer_class = PagerSerializer 11 pagination_class = PageNumberPagination 12 13 def get(self, request, *args, **kwargs): 14 roles = self.get_queryset() # queryset = models.Role.objects.all() 15 pager_roles = self.paginate_queryset(roles) # pagination_class = PageNumberPagination实例化 16 ser = self.get_serializer(instance=pager_roles, many=True) # serializer_class = PagerSerializer序列化 17 return Response(ser.data) 18 19 # -----------------------------继承GenericViewSet-------------------------------- 20 21 22 from rest_framework.viewsets import GenericViewSet 23 24 25 class View2View(GenericViewSet): 26 """ 27 class GenericViewSet(ViewSetMixin, generics.GenericAPIView) 28 裂开了这里 ViewSetMixin--->as_view重写了 29 """ 30 queryset = models.Role.objects.all() 31 serializer_class = PagerSerializer 32 pagination_class = PageNumberPagination 33 34 def list(self, request, *args, **kwargs): 35 roles = self.get_queryset() # queryset = models.Role.objects.all() 36 pager_roles = self.paginate_queryset(roles) # pagination_class = PageNumberPagination实例化 37 ser = self.get_serializer(instance=pager_roles, many=True) # serializer_class = PagerSerializer序列化 38 return Response(ser.data) 39 40 # -----------------------------继承ListModelMixin, CreateModelMixin---------------- 41 from rest_framework.viewsets import GenericViewSet 42 from rest_framework.mixins import ListModelMixin, CreateModelMixin 43 44 45 class View3View(ListModelMixin, CreateModelMixin, GenericViewSet): 46 queryset = models.Role.objects.all() 47 serializer_class = PagerSerializer 48 pagination_class = PageNumberPagination 49 50 51 # -----------------------------继承ModelViewSet------------------------------------ 52 from rest_framework.viewsets import ModelViewSet 53 54 55 class View4View(ModelViewSet): 56 queryset = models.Role.objects.all() 57 serializer_class = PagerSerializer 58 pagination_class = PageNumberPagination
视图分层流程图
总结:
a.基本增删改查 ModelViewSet
b.复杂逻辑用GenericViewSet或者APIView