rest_framework 分页三种

1.分页
    a. 分页 看第n页 每页显示n条数据:
    b. 分页 在某个位置 向后查看多少条数据
    c. 加密分页 上一页和下一页

    本质:查看 记住页码id的最大值和最小值 通过其来准确扫描
    过去的话 会从id 1到n扫描

    第一种分页
api下的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里写
from django.contrib import admin
from api import views
from django.conf.urls import url
urlpatterns = [
    url(r'^pager1/', views.Pager1View.as_view())
]


视图里写
from rest_framework.response import Response
from api.utils.serializers.pager import PagerSerialiser
from rest_framework.pagination import PageNumberPagination

class MyPageNumberPagination(PageNumberPagination):
    page_size=2  #默认两个
    page_size_query_param = 'size'  #传一个size参数 一页显示多少  http://127.0.0.1:8000/pager1/?page=1&size=3
    max_page_size = 5  #一页显示最大5个

    page_query_param = 'page'  #页码



class Pager1View(APIView):

    def get(self,request, *args,**kwargs):
        #获取所有数据
        roles = models.Role.objects.all()
        #创建分页对象
        pg = MyPageNumberPagination()
        #在数据库中获取分页数据
        pager_roles = pg.paginate_queryset(queryset=roles, request=request,view=self)

        print(pager_roles)
        #对分页数据进行序列化
        ser = PagerSerialiser(instance=pager_roles, many=True)

        return pg.get_paginated_response(ser.data)  #返回上一页或者下一页


第二种分页 索引值最大最小值分页
视图函数
from django.shortcuts import render,HttpResponse
import json
# Create your views here.
from rest_framework.views import APIView
from rest_framework import serializers
from api import models
class UserInfoserializer(serializers.ModelSerializer):

    group = serializers.HyperlinkedIdentityField(view_name='gp',lookup_field='group_id',lookup_url_kwarg='pk')
    class Meta:
        model = models.UserInfo
        # fields = '__all__'
        fields = ['group']
        depth = 0 #0到3 4层

class UserInfoView(APIView):
    def get(self,request, *arg, **kwargs):
        users = models.UserInfo.objects.all()
        ser = UserInfoserializer(instance=users, many=True,context={'request': request})  #必须添加 context = {'request':request}

        ret = json.dumps(ser.data, ensure_ascii = False)
        return HttpResponse(ret)



class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserGroup
        fields = '__all__'

class GroupView(APIView):

    def get(self,request,*args,**kwargs):
        pk = kwargs.get('pk')
        obj = models.UserGroup.objects.filter(pk=pk).first()

        ser = GroupSerializer(instance=obj, many=False)
        ret = json.dumps(ser.data,ensure_ascii=False)

        return HttpResponse(ret)

from rest_framework.response import Response
from api.utils.serializers.pager import PagerSerialiser
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination

class MyPageNumberPagination(PageNumberPagination):
    page_size=2  #默认两个
    page_size_query_param = 'size'  #传一个size参数 一页显示多少  http://127.0.0.1:8000/pager1/?page=1&size=3
    max_page_size = 5  #一页显示最大5个

    page_query_param = 'page'  #页码

class MyPagenumber2Pagination(LimitOffsetPagination):
    default_limit = 3
    limit_query_param = 'limit'  #每页多少条数据
    offset_query_param = 'offset'  #第几个索引开始
    max_limit = 5

class Pager1View(APIView):

    def get(self,request, *args,**kwargs):
        #获取所有数据
        roles = models.Role.objects.all()
        #创建分页对象
        pg = MyPagenumber2Pagination()
        #在数据库中获取分页数据
        pager_roles = pg.paginate_queryset(queryset=roles, request=request,view=self)

        print(pager_roles)
        #对分页数据进行序列化
        ser = PagerSerialiser(instance=pager_roles, many=True)

        # return pg.get_paginated_response(ser.data)  #返回上一页或者下一页

        return Response(ser.data)


    分页第三种:cursion

from rest_framework.response import Response
from api.utils.serializers.pager import PagerSerialiser
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination

class MyPageNumberPagination(PageNumberPagination):
    page_size=2  #默认两个
    page_size_query_param = 'size'  #传一个size参数 一页显示多少  http://127.0.0.1:8000/pager1/?page=1&size=3
    max_page_size = 5  #一页显示最大5个

    page_query_param = 'page'  #页码

class MyPagenumber2Pagination(LimitOffsetPagination):
    default_limit = 3
    limit_query_param = 'limit'  #每页多少条数据
    offset_query_param = 'offset'  #第几个索引开始
    max_limit = 5

class MyPagenumber3Pagination(CursorPagination):
    cursor_query_param = 'cursor'
    page_size = 2
    ordering = 'id'
    page_size_query_param = None
    max_page_size = None

class Pager1View(APIView):

    def get(self,request, *args,**kwargs):
        #获取所有数据
        roles = models.Role.objects.all()
        #创建分页对象
        pg = MyPagenumber3Pagination()
        #在数据库中获取分页数据
        pager_roles = pg.paginate_queryset(queryset=roles, request=request,view=self)

        print(pager_roles)
        #对分页数据进行序列化
        ser = PagerSerialiser(instance=pager_roles, many=True)

        return pg.get_paginated_response(ser.data)  #返回上一页或者下一页

 

posted @ 2018-07-21 14:37  想翻身的猫  阅读(1498)  评论(0编辑  收藏  举报