mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-28 13:03:45 +03:00
feat: add pagination to viewsets
This commit is contained in:
parent
f113ab6b68
commit
e9352e446f
|
@ -26,6 +26,7 @@ from django.views.decorators.csrf import csrf_exempt
|
||||||
from rest_framework import generics, mixins, views
|
from rest_framework import generics, mixins, views
|
||||||
from rest_framework.decorators import MethodMapper
|
from rest_framework.decorators import MethodMapper
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
|
from rest_framework.settings import api_settings
|
||||||
|
|
||||||
|
|
||||||
def _is_extra_action(attr):
|
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.
|
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):
|
class GenericViewSet(ViewSetMixin, generics.GenericAPIView):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user