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

 

posted @ 2018-07-22 10:25  想翻身的猫  阅读(312)  评论(0编辑  收藏  举报