Итак, я создал собственный класс разбиения на страницы, расширяющий PageNumberPagination
from rest_framework.pagination import PageNumberPagination
def replace_query_param(url, key, val):
"""
Given a URL and a key/val pair, set or replace an item in the query
parameters of the URL, and return the new URL.
"""
(scheme, netloc, path, query, fragment) = parse.urlsplit(force_str(url))
scheme = "https"
netloc = "api.example.com"
query_dict = parse.parse_qs(query, keep_blank_values=True)
query_dict[force_str(key)] = [force_str(val)]
query = parse.urlencode(sorted(list(query_dict.items())), doseq=True)
return parse.urlunsplit((scheme, netloc, path, query, fragment))
def remove_query_param(url, key):
"""
Given a URL and a key/val pair, remove an item in the query
parameters of the URL, and return the new URL.
"""
(scheme, netloc, path, query, fragment) = parse.urlsplit(force_str(url))
scheme = "https"
netloc = "api.example.com"
query_dict = parse.parse_qs(query, keep_blank_values=True)
query_dict.pop(key, None)
query = parse.urlencode(sorted(list(query_dict.items())), doseq=True)
return parse.urlunsplit((scheme, netloc, path, query, fragment))
class LargeResultsSetPagination(PageNumberPagination):
page_size = 1000
page_size_query_param = 'per_page'
max_page_size = 1000
def get_next_link(self):
if not self.page.has_next():
return None
url = self.request.build_absolute_uri()
page_number = self.page.next_page_number()
return replace_query_param(url, self.page_query_param, page_number)
def get_previous_link(self):
if not self.page.has_previous():
return None
url = self.request.build_absolute_uri()
page_number = self.page.previous_page_number()
if page_number == 1:
return remove_query_param(url, self.page_query_param)
return replace_query_param(url, self.page_query_param, page_number)
Теперь я использую этот класс разбиения на страницы во всех своих ViewSets
class TestViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
queryset = Test.objects.all().order_by("pk")
serializer_class = test_serializers.TestSerializer
pagination_class = LargeResultsSetPagination
search_fields = ['name', 'description', 'follow_up', 'follow_up_type']
filter_backends = (filters.SearchFilter,)
И он выполняет свою работу. , оригинальное вдохновение { ссылка }