1 # TODO 总结视图关系使用选择 2 """ 3 1、总结视图使用: 4 a、增删改查 ModelviewSet 5 b、增删 CreateModelMixin,DestroyModelMixin,GenericViewSet 如果需要支持增删可继承这几个操作 6 c、复杂逻辑 GenericViewSet 或 APIView 一般开发可选择这2个开发 7 d、APIView 比较原生,纯净,可以需求选择这个做开发, 8 9 10 2、根据实际需求选择不同的视图模式,和对应的路由系统 15 路由ruoter.resigiter 自动生成增删改查,根据自己的需要选择是否需要自动生成增删改查地址 16 17 """ 18 """ 19 from rest_framework.response import Response 返回的 字典可以自动序列化为json 20 class demoview(APIView): 21 def get(self, request, *args, **kwargs): 22 return Respons({"result": "data"}) 23 """ 24 25 # TODO GenericAPIView 基本方法 26 # TODO def get 开始执行 27 # re_path(r'^(?P<version>[v1|v2]+)/order/$', views.OrderVies.as_view()), 28 """ 29 from rest_framework.generics import GenericAPIView 30 class view1View(GenericAPIView): 31 queryset = UserInfo.objects.all() 32 serializer_class = UserInfoSerializer 33 pagination_class = PageNumberPagination 34 35 def get(self, request, *args, **kwargs): 36 # 获取数据 37 roles = self.get_queryset() 38 # 分页 39 pager_roles = self.paginate_queryset(roles) 40 ret = self.get_serializer(instance=pager_roles, many=True) 41 return Response(ret.data) 42 """ 43 44 # TODO 继承 分叉 ViewSetMixin, generics.GenericAPIView 45 # TODO ViewSetMixin 需要给路由地址as_view传参数 as_view({"get": "list"}) 46 # TODO def list 开始执行 47 # 需要在路由地址给as_view 传参数 re_path(r'^(?P<version>[v1|v2]+)/order/$', views.OrderVies.as_view({"get": "list"})), 48 """ 49 from rest_framework.viewsets import GenericViewSet 50 51 class view1View(GenericViewSet): 52 queryset = UserInfo.objects.all() 53 serializer_class = UserInfoSerializer 54 pagination_class = PageNumberPagination 55 56 def list(self, request, *args, **kwargs): 57 # 获取数据 58 roles = self.get_queryset() 59 # 分页 60 pager_roles = self.paginate_queryset(roles) 61 ret = self.get_serializer(instance=pager_roles, many=True) 62 return Response(ret.data) 63 """ 64 65 # TODO ListModelMixin 不用些 def list 自动显示列表信息 66 # class view1View(ListModelMixin, CreateModelMixin, GenericViewSet): 67 # 相当于把方法都写到类里面了,系统自带的增删改查的方法,对应的Url地址参数也需要修改为对应的参数 68 # 路由地址 re_path(r'^(?P<version>[v1|v2]+)/order/$', views.OrderVies.as_view({"get": "list"})), 69 """ 70 from rest_framework.viewsets import GenericViewSet, ModelViewSet 71 from rest_framework.mixins import ListModelMixin, CreateModelMixin, UpdateModelMixin 72 73 class view1View(ListModelMixin, GenericViewSet): 74 queryset = UserInfo.objects.all() 75 serializer_class = UserInfoSerializer 76 pagination_class = PageNumberPagination 77 78 """ 79 80 # TODO ModelViewSet 支持增删改查 81 # 继承 class ModelViewSet(mixins.CreateModelMixin, 创建数据 82 # mixins.RetrieveModelMixin, 获取单条数据 需要传ID 83 # mixins.UpdateModelMixin, 更新数据 需要传ID 84 # mixins.DestroyModelMixin, 删除数据 需要传ID 85 # mixins.ListModelMixin, 获取所有数据列表页 86 # GenericViewSet): 87 88 # 继承增删改查的视图类,对应的Url地址参数也需要修改为对应的参数 89 # 路由地址 re_path(r'^(?P<version>[v1|v2]+)/order/$', views.OrderVies.as_view({"get": "list"})), 90 # 增加对应的 增删改查地址,并获取id pk做对应的数据修改 91 # re_path(r'^v1/view4/(?P<pk>\d+)/$', views.View4View.as_view( 92 # {'get': 'retrieve', 93 # 'delete': 'destroy', 94 # 'put': 'update', 95 # 'patch': 'partial_update'})), 96 # TODO 自动生成 路由规则 获取ID 对应上面的手写规则 97 """ 98 from rest_framework import routers 99 router = routers.DefaultRouter() 100 router.register(r"order", views.order) 101 102 re_path(r"^", include(router.urls)) 103 """ 104 """ 105 from rest_framework.viewsets import ModelViewSet 106 class view1View(ModelViewSet): 107 queryset = UserInfo.objects.all() 108 serializer_class = UserInfoSerializer 109 pagination_class = PageNumberPagination 110 """ 111