• drf----分页


    PageNumberPagination

    1,配置

    • REST_FRAMEWORK = {
          "PAGE_SIZE": 2,
          "DEFAULT_PAGINATION_CLASS": 'rest_framework.paginate.PageArticleSerializer',
      }
      

    2,url

    • 
      from django.conf.urls import url
      from django.contrib import admin
      from API import views
      urlpatterns = [
          url(r'^admin/', admin.site.urls),
          url(r'^new/article/$', views.NewArticleView.as_view(),),
          url(r'^new/article/(?P<pk>d+)/$', views.NewArticleView.as_view(),),
          url(r'^page/article/$', views.PageArticleView.as_view(), ),
      
      ]
      
      

    3,字段处理

    • from rest_framework.pagination import PageNumberPagination
      class PageArticleSerializer(serializers.ModelSerializer):
      	class Meta:
              model = models.Article
              fields = "__all__"
      

    4,视图

    • class PageArticleView(APIView):
          def get(self, request, *args, **kwargs):
              queryset = models.Article.objects.all()
      
              # 方式一 仅数据
              """
              # 调用分页对象.paginate_queryset方法进行分页,得到的结果是分页之			后的数据
              page_obj = PageNumberPagination()
              # result就是分页玩的一部分数据
              result = page_obj.paginate_queryset(queryset, request, self)
              # 序列化分页之后的数据
              ser = serializer.PageArticleSerializer(instance=result, many=True)
              return Response(ser.data)
              """
      
              # 方式二 数据+分页信息
              """
              page_obj = PageNumberPagination()
              result = page_obj.paginate_queryset(queryset, request, self)
              ser = serializer.PageArticleSerializer(instance=result, many=True)
              return page_obj.get_paginated_response(ser.data)
              """
      
              # 方式三 数据加部分分页信息,Response({可定制})
              page_obj = PageNumberPagination()
              result = page_obj.paginate_queryset(queryset, request, self)
              ser = serializer.PageArticleSerializer(instance=result, many=True)
              return Response({
                  "count": page_obj.page.paginator.count,
                  "result": ser.data
              })
      
      

    LimitOffsetPagination

    • from rest_framework.pagination import PageNumberPagination
      from rest_framework.pagination import LimitOffsetPagination
      from rest_framework import serializers
      
      
      class PageArticleSerializer(serializers.ModelSerializer):
          class Meta:
              model = models.Article
      
      
      fields = "__all__"
      
      
      class HulaLimitOffsetPagination(LimitOffsetPagination):
          max_limit = 2
      
      
      class PageArticleView(APIView):
          def get(self, request, *args, **kwargs):
              queryset = models.Article.objects.all()
              page_object = HulaLimitOffsetPagination()
              result = page_object.paginate_queryset(queryset, request, self)
              ser = PageArticleSerializer(instance=result, many=True)
              return Response(ser.data)
      
      
    希望你眼眸有星辰,心中有山海,从此以梦为马,不负韶华
  • 相关阅读:
    shell脚本编程-结构化命令3-while、until命令
    shell脚本编程-结构化命令2-for命令
    sscanf解析复杂字符串,双引号通配符的使用问题
    shell脚本编程-结构化命令1-分支语句
    shell脚本编程基础
    linux系统管理的基本命令2
    linux系统管理的基本命令
    redis
    Eclipse启动报错
    java斗地主发牌源码
  • 原文地址:https://www.cnblogs.com/daviddd/p/11918368.html
Copyright © 2020-2023  润新知