zoukankan      html  css  js  c++  java
  • rest_framework 视图

    视图:
        a.过去
            class PagerView(View):
                pass
    
        b.现在
            class Pager1View(APIView):
                pass
    
        c.rest_framework  基本没用
    
        from rest_framework.generics import GenericAPIView
        class V1View(GenericAPIView):
            queryset = models.Role.objects.all()
            serializer_class = PagerSerialiser
            pagination_class = PageNumberPagination
    
            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)
    
        d.GenericViewset(ViewSetMixin, genrics.GenericAPIView):
    
        urls.py
        url(r'^v1/$',views.V1View.as_view({'get':'list'})),  #可以改变as_view 视图中的函数中的get方法 改成了list方法
    
        from rest_framework.viewsets import GenericViewSet
        class V1View(GenericViewSet):
            def list(self,request,*args,**kwargs):
                return Response('123')
    
    
        最方便的视图方法 增删改查局部更新和GenericViewSet
        视图中
        from rest_framework.response import Response
        from api.utils.serializers.pager import PagerSerialiser
        from rest_framework.pagination import PageNumberPagination
        from rest_framework.viewsets import ModelViewSet
        class V1View(ModelViewSet):
        """
        class ModelViewSet(
                       mixins.CreateModelMixin,  #不需要传id
                       mixins.RetrieveModelMixin,
                       mixins.UpdateModelMixin,
                       mixins.DestroyModelMixin,
                       mixins.ListModelMixin,    #不需要传id
                       GenericViewSet):
        """
        queryset = models.Role.objects.all()
        serializer_class = PagerSerialiser
        pagination_class = PageNumberPagination
    
    
    
        urls.py  路由系统
        from django.contrib import admin
        from django.urls import path
        from api import views
        from django.conf.urls import url
        urlpatterns = [
            url(r'^v1/$',views.V1View.as_view({'get':'list','post':'create'})),
            url(r'^v1/(?P<pk>d+)/$',views.V1View.as_view({'get':'retrieve','delete':'destroy','put':'update','patch':'partial_update'})),
        ]
    
        utils.serializers.pager.py
        from rest_framework import serializers
        from api import models
        class PagerSerialiser(serializers.ModelSerializer):
            class Meta:
                model = models.Role
                fields = "__all__"

    视图总结部分

    路由系统
    url(r'^v1/$,views.V1View.as_view({'get':'list',post':'create'})),
    url(r'^v1/(?P<pk>d+)/$', views.V1View.as_view({'get':'retrieve','delete':'destroy','put':'update','patch':'partial_update'})),

    视图
    from api.utils.serializers.pager import PagerSerialiser
    from rest_framework.viewsets import GenericViewSet, ModelViewSet
    from rest_framework.mixins import ListModelMixin , createModelMixin

    class V1View(ModelViewSet): #ModelViewSet包含增删改查局部更新和GenericViewSet
            queryset = models.Role.objects.all()
    serializer_class = PagerSerialiser
    pagination_class = PageNumberPagination
  • 相关阅读:
    Valid Palindrome
    Path Sum II
    Reverse Integer
    Palindrome Number
    ZigZag Conversion
    函数调用堆栈 涉及汇编(转)
    关于字符指针和字符数组初始化的问题
    Remove Element(第一种方法参考别人)
    c内存分配(转)
    int *ptr=(int *)(&a+1)问题的探讨
  • 原文地址:https://www.cnblogs.com/Liang-jc/p/9349288.html
Copyright © 2011-2022 走看看