Skip to content

Commit 427917a

Browse files
committed
feat: add pagination for viewsets
1 parent f113ab6 commit 427917a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

rest_framework/viewsets.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from rest_framework import generics, mixins, views
2727
from rest_framework.decorators import MethodMapper
2828
from rest_framework.reverse import reverse
29+
from rest_framework.settings import api_settings
2930

3031

3132
def _is_extra_action(attr):
@@ -214,7 +215,35 @@ class ViewSet(ViewSetMixin, views.APIView):
214215
"""
215216
The base ViewSet class does not provide any actions by default.
216217
"""
217-
pass
218+
219+
pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
220+
221+
@property
222+
def paginator(self):
223+
"""
224+
The paginator instance associated with the view, or `None`.
225+
"""
226+
if not hasattr(self, "_paginator"):
227+
if self.pagination_class is None:
228+
self._paginator = None
229+
else:
230+
self._paginator = self.pagination_class()
231+
return self._paginator
232+
233+
def paginate_queryset(self, queryset):
234+
"""
235+
Return a single page of results, or `None` if pagination is disabled.
236+
"""
237+
if self.paginator is None:
238+
return None
239+
return self.paginator.paginate_queryset(queryset, self.request, view=self)
240+
241+
def get_paginated_response(self, data):
242+
"""
243+
Return a paginated style `Response` object for the given output data.
244+
"""
245+
assert self.paginator is not None
246+
return self.paginator.get_paginated_response(data)
218247

219248

220249
class GenericViewSet(ViewSetMixin, generics.GenericAPIView):

0 commit comments

Comments
 (0)