一、分页组件
1、PageNumberPagination
a、全局配置
导入模块
from rest_framework.pagination import PageNumberPagination
在视图类中配置
def get(self, request):
# 注意:数据quweyset book_list = Book.objects.get_queryset().order_by('pk') # 实例化 pnp = PageNumberPagination() # 调用方法 pnp_list = pnp.paginate_queryset(book_list, request, self) book_serialize = BookModelSerialize(pnp_list, many=True, context={'request': request}) return Response(book_serialize.data)
在配置文件中,修改全局page_size
REST_FRAMEWORK = { 'PAGE_SIZE': 1, }
注意:queryset,book_list
b、局部分页
自定义分页类,继承PageNumberPagination
class MyPageNumberPagination(PageNumberPagination): page_size = 1 page_query_param = 'page' # 临时分页的参数 size=当前页面显示的数量 page_size_query_param = 'size' # 临时分页size的最大值 max_page_size = 3
视图类
class BookView(APIView): # parser_classes = [JSONParser] # 查看书籍, 返回所有数据(书籍信息) def get(self, request): # 获取book的所有数据 book_list = Book.objects.get_queryset().order_by('pk') # 实例化对象 pnp = MyPageNumberPagination() pnp_list = pnp.paginate_queryset(book_list, request, self) book_serialize = BookModelSerialize(pnp_list, many=True) return Response(book_serialize.data)
2、LimitOffsetPagination
导入模块
from rest_framework.pagination import LimitOffsetPagination
a、全局设置和上面相似,具体看源码
b、局部设置
分页类
class MyLimitOffsetPagination(LimitOffsetPagination): default_limit = 1 # limit 分页 limit=n ,一页有n条数据 limit_query_param = 'limit' # offset 偏移的数据量 offset=n,从第n条数据开始分页 offset_query_param = 'offset'
视图类
class BookView(APIView): # parser_classes = [JSONParser] # 查看书籍, 返回所有数据(书籍信息) def get(self, request): # 获取book的所有数据 book_list = Book.objects.get_queryset().order_by('pk') # 实例化对象 pnp = MyLimitOffsetPagination() pnp_list = pnp.paginate_queryset(book_list, request, self) book_serialize = BookModelSerialize(pnp_list, many=True) return Response(book_serialize.data)
注意:limit是每页的数据量,offset是从第几条数据开始分页,不包含此条数据
3、终极视图组件分页
a、局部
注意:认证、权限可以有多个,分页器只能有一个
class AuthorViewSet(viewsets.ModelViewSet): # permission_classes = [PermissionSet] # authentication_classes = [ModelToken] pagination_class = MyLimitOffsetPagination queryset = Author.objects.all() serializer_class = AuthorModelSerializer
b、全局
REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'app01.components.Pagination.MyPageNumberPagination' }
设置局部无分页
pagination_class = PageNumberPagination
源码解析
ModelViewSet -> mixins.ListModelMixin -> paginate_queryset
ModelViewSet => GenericViewSet -> generics.GenericAPIView -> paginate_queryset -> paginator -> pagination_class() -> api_settings.DEFAULT_PAGINATION_CLASS
3.CursorPagination (加密分页)
分页器
class MyCursorPagination(CursorPagination): cursor_query_param = 'cursor' page_size = 1 ordering = '-id' # 重写要排序的字段
视图
class ArticleList(APIView): def get(self, request, *args, **kwargs): res = {"code": 0} article_list = models.Article.objects.all().order_by("id") # 分页 page_obj = MyCursorPagination() page_article = page_obj.paginate_queryset(queryset=article_list, request=request, view=self) ser_obj = ArticleSerializer(page_article, many=True) res["data"] = ser_obj.data # return Response(res) return page_obj.get_paginated_response(res)
二、相应器
restframework 自定义的
from rest_framework.response import Response
注意:在浏览器和postman显示数据的格式不一样