「Django」rest_framework学习系列-分页

分页
a.分页,看第N页,每页显示N条数据
方式一:使用PageNumberPagination创建分页对象,配合settings全局配置

views设置

from rest_framework.pagination import PageNumberPagination

class PagesView(APIView):
    def get(self,request,*args,**kwargs):
    #获取所有数据
        m = models.UserInfo.objects.all()
    #创建分页对象
        pg = PageNumberPagination()
    #在数据库中获取分页的数据
        pa_info = pg.paginate_queryset(queryset=m,request=request,view=self)
    #对数据进行序列化,此处参照序列化部分
        ser = pages.PagesSerializer(instance=pa_info,many=True)
    #自带的返回格式,效果更好
      return pg.get_paginated_response(ser.data)
    #return Response(ser.data)

settings设置

REST_FRAMEWORK = {
    'PAGE_SIZE':2,
}

返回结果

"count": 10,
"next": "http://127.0.0.1:8000/api/v1/pages/?page=3&size=2",
"previous": "http://127.0.0.1:8000/api/v1/pages/?size=2",

方式二:不使用全局配置,继承PageNumberPagination做自定制类,其余同上

自定制类:

from rest_framework.pagination import PageNumberPagination
class MyPageNumberPagination(PageNumberPagination):
    page_size = 3 #设置每页显示个数
    page_size_query_param = 'size' #设置前段传参字段
    max_page_size = 5 #设置最大显示个数
    page_query_param = 'page' #前段页面传参字段
View Code

urls:

http://127.0.0.1:8000/api/v1/pages/?page=2&size=2

views:

class PagesView(APIView):
    def get(self,request,*args,**kwargs):
        #获取所有数据
        m = models.UserInfo.objects.all()
        #创建分页对象,与方式一不同
        pg = MyPageNumberPagination()
        #在数据库中获取分页的数据
        pa_info = pg.paginate_queryset(queryset=m,request=request,view=self)
        #对数据进行序列化
        ser = pages.PagesSerializer(instance=pa_info,many=True)
        return pg.get_paginated_response(ser.data)    
View Code

b.分页,看N个位置,向后查看N条数据,方式同a-2,自定制类继承LimitOffsetPagination类

自定制类:

from rest_framework.pagination import LimitOffsetPagination
class MyPageNumberPaginationNew(LimitOffsetPagination):
    default_limit = 4 #设置每页显示个数
    limit_query_param = 'limit' #设置前端显示数量的传参字段
    offset_query_param = 'offset' #设置前端页面开始数字传参字段
    max_limit = 5
View Code

urls:

http://127.0.0.1:8000/api/v1/pages/?limit=3&offset=2

views:

class PagesView(APIView):
    def get(self,request,*args,**kwargs):
        #获取所有数据
        m = models.UserInfo.objects.all()
        #创建分页对象,实例化上面的类
        pg = MyPageNumberPaginationNew()
        #在数据库中获取分页的数据
        pa_info = pg.paginate_queryset(queryset=m,request=request,view=self)
        #对数据进行序列化
        ser = pages.PagesSerializer(instance=pa_info,many=True)
        return pg.get_paginated_response(ser.data)
View Code

c.加密分页,上一页和下一页,方式同上,自定制类继承CursorPagination类

自定制类:

from rest_framework.pagination import CursorPagination

class MyPageNumberPaginationCursor(CursorPagination):
    cursor_query_param = 'cursor'
    page_size = 5
    page_size_query_param = 'size'
    max_page_size = 5
    ordering = 'id' #数据排序方式
MyPageNumberPaginationCursor

urls:

http://127.0.0.1:8000/api/v1/pages/?cursor=cD0y&size=2

views:

class PagesView(APIView):
    def get(self,request,*args,**kwargs):
        #获取所有数据
        m = models.UserInfo.objects.all()
        #创建分页对象,同上,只修改实例类即可
        pg = MyPageNumberPaginationCursor()
        #在数据库中获取分页的数据
        pa_info = pg.paginate_queryset(queryset=m,request=request,view=self)
        #对数据进行序列化
        ser = pages.PagesSerializer(instance=pa_info,many=True)
        return pg.get_paginated_response(ser.data)
View Code

 

posted @ 2019-02-19 17:41  子谦0618  阅读(117)  评论(0编辑  收藏  举报