custom_pagination.py 855 B

1234567891011121314151617181920212223242526272829
  1. from collections import OrderedDict # requires Python 2.7 or later
  2. from django.core.paginator import Paginator
  3. from django.utils.functional import cached_property
  4. from rest_framework.pagination import PageNumberPagination
  5. from rest_framework.response import Response
  6. class FasterDjangoPaginator(Paginator):
  7. @cached_property
  8. def count(self):
  9. return 50
  10. class FastPaginationWithoutCount(PageNumberPagination):
  11. """Experimental, for cases where a SELECT COUNT is redundant"""
  12. django_paginator_class = FasterDjangoPaginator
  13. def get_paginated_response(self, data):
  14. return Response(
  15. OrderedDict(
  16. [
  17. ("next", self.get_next_link()),
  18. ("previous", self.get_previous_link()),
  19. ("results", data),
  20. ]
  21. )
  22. )