feat: add pagination for viewsets

This commit is contained in:
Paulo Victor 2024-08-07 14:20:39 -03:00
parent f113ab6b68
commit 427917a47b

View File

@ -26,6 +26,7 @@ from django.views.decorators.csrf import csrf_exempt
from rest_framework import generics, mixins, views
from rest_framework.decorators import MethodMapper
from rest_framework.reverse import reverse
from rest_framework.settings import api_settings
def _is_extra_action(attr):
@ -214,7 +215,35 @@ class ViewSet(ViewSetMixin, views.APIView):
"""
The base ViewSet class does not provide any actions by default.
"""
pass
pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
@property
def paginator(self):
"""
The paginator instance associated with the view, or `None`.
"""
if not hasattr(self, "_paginator"):
if self.pagination_class is None:
self._paginator = None
else:
self._paginator = self.pagination_class()
return self._paginator
def paginate_queryset(self, queryset):
"""
Return a single page of results, or `None` if pagination is disabled.
"""
if self.paginator is None:
return None
return self.paginator.paginate_queryset(queryset, self.request, view=self)
def get_paginated_response(self, data):
"""
Return a paginated style `Response` object for the given output data.
"""
assert self.paginator is not None
return self.paginator.get_paginated_response(data)
class GenericViewSet(ViewSetMixin, generics.GenericAPIView):