From 4635bbbebaf27ee715a23a532707b5d03c3d0096 Mon Sep 17 00:00:00 2001 From: mortezaei Date: Thu, 1 Jan 2026 10:45:45 +0330 Subject: [PATCH] Implement custom pagination class for REST framework and update settings to use it --- config/settings/base.py | 2 +- utils/pagination.py | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) 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