django-restframework之分页器组件

django-restframework之分页器组件

一 前言

本篇博客介绍 django-restframework 的分页器组件,在 restframework 框架中的分页器有三种,分别是简单分页、偏移分页、加密分页(url 经过加密)。接下来分别介绍三种分页器的使用方法。

二 简单分页

查看第 n 页,每页显示 n 条。

page_size = api_settings.PAGE_SIZE :每页显示条数
page_query_param = 'page' :查询的页码数
page_size_query_param = None :每页最大显示条数

如果我要的数据是第一页显示10条:http://127.0.0.1:8000/books/?ccc=10&page=1
如果我要的是第二页显示5条:http://127.0.0.1:8000/books/?ccc=5&page=2
max_page_size = 8 :控制最大显示多少条
如果我想第一页显示10000条,这种不合理,通过这个参数控制,最大显示8条
使用:
page_sipmle=PageNumberPagination()
page_sipmle.page_size=4

​ page_sipmle.page_query_param='aaa'

​ page_sipmle.page_size_query_param='ccc'
​ page_sipmle.max_page_size=8
​ book_list = models.Book.objects.all()
​ page_list = page_sipmle.paginate_queryset(book_list,request,self)
​ book_ser = BookSerializer(instance=page_list, many=True)

简单分页
from rest_framework.pagination import PageNumberPagination
class Books(APIView):
    def get(self, request):
        response = {'status': 100, 'msg': '成功'}
        # 生成一个PageNumberPagination的对象
        page_sipmle=PageNumberPagination()
        page_sipmle.page_size=4
        # page_sipmle.page_query_param='aaa'
        page_sipmle.page_size_query_param='ccc'
        page_sipmle.max_page_size=8
        book_list = models.Book.objects.all()
        page_list = page_sipmle.paginate_queryset(book_list,request,self)
        book_ser = BookSerializer(instance=page_list, many=True)

        print(book_ser.data)
        response['books'] = book_ser.data
        return Response(response)

三偏移分页

default_limit = api_settings.PAGE_SIZE :默认偏移的条数 5

limit_query_param = 'limit' :偏移的条数

offset_query_param = 'offset' :是标杆

max_limit = None :最大偏移的条数

举例
-http://127.0.0.1:8000/books/ 结果是:从1到5
-http://127.0.0.1:8000/books/?limit=7 结果是:从1到7
-http://127.0.0.1:8000/books/?limit=2&offset=6 结果是:从7到8,两条
-http://127.0.0.1:8000/books/?offset=6 结果是:从7到11,5条

# 偏移分页
from rest_framework.pagination import LimitOffsetPagination
class Books(APIView):
    def get(self, request):
        response = {'status': 100, 'msg': '成功'}
        # 生成一个PageNumberPagination的对象
        page_limit=LimitOffsetPagination()
        page_limit.default_limit=6
        page_limit.max_limit=5
        book_list = models.Book.objects.all()
        page_list = page_limit.paginate_queryset(book_list,request,self)
        book_ser = BookSerializer(instance=page_list, many=True)
        print(book_ser.data)
        response['books'] = book_ser.data
        return Response(response)

四 加密分页

游标分页:原理:速度最快,但是只能往前走和往后走

cursor_query_param = 'cursor'

page_size = api_settings.PAGE_SIZE :默认每页显示的条数

ordering = 'id' :按谁排序

# 加密分页
from rest_framework.pagination import CursorPagination
class Books(APIView):
    def get(self, request):
        response = {'status': 100, 'msg': '成功'}
        # 生成一个PageNumberPagination的对象
        page_cursor=CursorPagination()
        page_cursor.page_size=5
        page_cursor.ordering="id"
        book_list = models.Book.objects.all()
        page_list = page_cursor.paginate_queryset(book_list,request,self)
        book_ser = BookSerializer(instance=page_list, many=True)
        print(book_ser.data)
        response['books'] = book_ser.data
        # return Response(response)
        return page_cursor.get_paginated_response(book_ser.data)
posted @ 2019-02-26 20:46  rsuxwvilc  阅读(70)  评论(0编辑  收藏  举报