一、使用方法

1.添加引擎

在全局配置settings.py模块的REST_FRAMEWORK字段中配置引擎和指定每页显示的数据条数

  1. REST_FRAMEWORK = {
  2. 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
  3. 'PAGE_SIZE': 5
  4. }

2.引入方式

使用self.paginate_queryset()方法进行分页,它需要传一个查询集对象参数,如果没有进行分页,则会返回None,因此需要做一个if判断,并使用self.get_paginated_response()序列化输出分页后的数据,该方法接收一个序列化器对象.data参数

  1. from rest_framework.response import Response
  2. from rest_framework import status
  3. from rest_framework.generics import GenericAPIView
  4. from .models import Projects
  5. from .serializers import ProjectsModelSerializer
  6. class ProjectsPage(GenericAPIView):
  7. '''
  8. 类视图
  9. '''
  10. queryset = Projects.objects.all()
  11. serializer_class = ProjectsModelSerializer
  12. def get(self, request):
  13. qs = self.filter_queryset(self.get_queryset())
  14. page = self.paginate_queryset(qs)
  15. if page:
  16. serializer_obj = self.get_serializer(instance=page, many=True)
  17. return self.get_paginated_response({'code': 0, 'res': "success", 'msg': serializer_obj.data})
  18. serializer_obj = self.get_serializer(instance=qs, many=True)
  19. return Response({'code': 0, 'res': "success", 'msg': serializer_obj.data}, status=status.HTTP_200_OK)

验证结果:
DRF -GenericAPIView 分页查询 - 图1

3.指定页展示指定的数据条数

在PageNumberPagination类中,我们可以对一些字段值进行重写,先看看源码
DRF -GenericAPIView 分页查询 - 图2

1.重写PageNumberPagination

我们直接新建一个pagination.py模块,在该模块中创建一个类PageNumberPagination父类,并重写类属性,并且自定义的属性优先级最高,如果全局配置中也有相关配置,则直接会被覆盖

  1. from rest_framework.pagination import PageNumberPagination
  2. class NewPagination(PageNumberPagination):
  3. page_size = 5
  4. max_page_size = 50
  5. page_size_query_param = 'size'

2.配置引擎

将新建的NewPagination的引用地址,配置到全局配置settings.py模块的REST_FRAMEWORK字段中,并将原先的注释掉

  1. REST_FRAMEWORK = {
  2. # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
  3. # 'PAGE_SIZE': 5,
  4. 'DEFAULT_PAGINATION_CLASS': 'utils.pagination.NewPagination'
  5. }

验证结果:
DRF -GenericAPIView 分页查询 - 图3

4.在视图中指定分页引擎类

在上面我们的分页引擎都是配置到全局中的,如果不希望所有的查询接口都支持该功能,我们可以只指定当前接口所属的视图,这时就不需要在全局配置中配置分页引擎,直接在视图中使用pagination_class类属性指定分页引擎

  1. from django.db import connection
  2. from django.http import Http404from rest_framework.response import Response
  3. from rest_framework import status
  4. from rest_framework.generics import GenericAPIView
  5. from utils.pagination import NewPagination
  6. from .models import Projects
  7. from .serializers import ProjectsModelSerializer
  8. class ProjectsPage(GenericAPIView):
  9. '''
  10. 类视图
  11. '''
  12. queryset = Projects.objects.all()
  13. serializer_class = ProjectsModelSerializer
  14. pagination_class = NewPagination
  15. def get(self, request):
  16. qs = self.filter_queryset(self.get_queryset())
  17. page = self.paginate_queryset(qs)
  18. if page:
  19. serializer_obj = self.get_serializer(instance=page, many=True)
  20. return self.get_paginated_response({'code': 0, 'res': "success", 'msg': serializer_obj.data})
  21. serializer_obj = self.get_serializer(instance=qs, many=True)
  22. return Response({'code': 0, 'res': "success", 'msg': serializer_obj.data}, status=status.HTTP_200_OK)