diff --git a/config/settings/base.py b/config/settings/base.py index de34cf0..883c813 100755 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -231,7 +231,7 @@ AUTH_PASSWORD_VALIDATORS = [ REST_FRAMEWORK = { - 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', + 'DEFAULT_PAGINATION_CLASS': 'utils.pagination.StandardResultsSetPagination', 'PAGE_SIZE': 16, # 'DEFAULT_AUTHENTICATION_CLASSES': [ # 'apps.account.auth_back.TokenAuthentication2', diff --git a/utils/pagination.py b/utils/pagination.py index 10b254b..39ab359 100644 --- a/utils/pagination.py +++ b/utils/pagination.py @@ -1,12 +1,23 @@ +from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination +from rest_framework.response import Response -from rest_framework.pagination import PageNumberPagination -from rest_framework.response import Response +class StandardResultsSetPagination(PageNumberPagination): + page_size = 16 + page_size_query_param = 'page_size' + max_page_size = 100 + + def get_paginated_response(self, data): + return Response({ + 'count': self.page.paginator.count, + 'next': self.get_next_link(), + 'previous': self.get_previous_link(), + 'results': data, + }) + - class NoPagination(PageNumberPagination): def paginate_queryset(self, queryset, request, view=None): - # Override to return all items instead of paginated ones self.count = len(queryset) self.request = request self.page = None @@ -14,10 +25,9 @@ class NoPagination(PageNumberPagination): return list(queryset) def get_paginated_response(self, data): - # Keep the structure but include all results return Response({ 'count': self.count, - 'next': None, # No next page - 'previous': None, # No previous page + 'next': None, + 'previous': None, 'results': data, }) \ No newline at end of file