MyPageNumber.py 754 B

123456789101112131415161718192021
  1. from rest_framework.pagination import PageNumberPagination
  2. from rest_framework.response import Response
  3. from collections import OrderedDict
  4. import math
  5. # 自定义分页
  6. class MyPageNumberPagination(PageNumberPagination):
  7. # 1,设置页面大小
  8. page_size_query_param = "page_size"
  9. # 2,设置页面最大值
  10. max_page_size = 5
  11. # 3,设置默认每页显示记录数
  12. page_size = 6
  13. def get_paginated_response(self, data):
  14. return Response(OrderedDict([
  15. ('count', self.page.paginator.count),
  16. ('count_page', math.ceil( self.page.paginator.count/self.page_size )),
  17. ('next', self.get_next_link()),
  18. ('previous', self.get_previous_link()),
  19. ('results', data)
  20. ]))