mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 13:04:03 +03:00
Simplify paginate_queryset method
This commit is contained in:
parent
9abaf77401
commit
5d01ae661f
|
@ -45,6 +45,8 @@ class GenericAPIView(views.APIView):
|
||||||
# serializer class will be constructed using this class as the base.
|
# serializer class will be constructed using this class as the base.
|
||||||
model_serializer_class = api_settings.DEFAULT_MODEL_SERIALIZER_CLASS
|
model_serializer_class = api_settings.DEFAULT_MODEL_SERIALIZER_CLASS
|
||||||
|
|
||||||
|
_paginator_class = Paginator
|
||||||
|
|
||||||
######################################
|
######################################
|
||||||
# These are pending deprecation...
|
# These are pending deprecation...
|
||||||
|
|
||||||
|
@ -85,11 +87,23 @@ class GenericAPIView(views.APIView):
|
||||||
context = self.get_serializer_context()
|
context = self.get_serializer_context()
|
||||||
return pagination_serializer_class(instance=page, context=context)
|
return pagination_serializer_class(instance=page, context=context)
|
||||||
|
|
||||||
def paginate_queryset(self, queryset, page_size, paginator_class=Paginator):
|
def paginate_queryset(self, queryset, page_size=None):
|
||||||
"""
|
"""
|
||||||
Paginate a queryset.
|
Paginate a queryset if required, either returning a page object,
|
||||||
|
or `None` if pagination is not configured for this view.
|
||||||
"""
|
"""
|
||||||
paginator = paginator_class(queryset, page_size,
|
deprecated_style = False
|
||||||
|
if page_size is not None:
|
||||||
|
# TODO: Deperecation warning
|
||||||
|
deprecated_style = True
|
||||||
|
else:
|
||||||
|
# Determine the required page size.
|
||||||
|
# If pagination is not configured, simply return None.
|
||||||
|
page_size = self.get_paginate_by()
|
||||||
|
if not page_size:
|
||||||
|
return None
|
||||||
|
|
||||||
|
paginator = self._paginator_class(queryset, page_size,
|
||||||
allow_empty_first_page=self.allow_empty)
|
allow_empty_first_page=self.allow_empty)
|
||||||
page_kwarg = self.kwargs.get(self.page_kwarg)
|
page_kwarg = self.kwargs.get(self.page_kwarg)
|
||||||
page_query_param = self.request.QUERY_PARAMS.get(self.page_kwarg)
|
page_query_param = self.request.QUERY_PARAMS.get(self.page_kwarg)
|
||||||
|
@ -103,13 +117,16 @@ class GenericAPIView(views.APIView):
|
||||||
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
|
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
|
||||||
try:
|
try:
|
||||||
page = paginator.page(page_number)
|
page = paginator.page(page_number)
|
||||||
return (paginator, page, page.object_list, page.has_other_pages())
|
|
||||||
except InvalidPage as e:
|
except InvalidPage as e:
|
||||||
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
|
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
|
||||||
'page_number': page_number,
|
'page_number': page_number,
|
||||||
'message': str(e)
|
'message': str(e)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if deprecated_style:
|
||||||
|
return (paginator, page, page.object_list, page.has_other_pages())
|
||||||
|
return page
|
||||||
|
|
||||||
def filter_queryset(self, queryset):
|
def filter_queryset(self, queryset):
|
||||||
"""
|
"""
|
||||||
Given a queryset, filter it with whichever filter backend is in use.
|
Given a queryset, filter it with whichever filter backend is in use.
|
||||||
|
@ -163,7 +180,6 @@ class GenericAPIView(views.APIView):
|
||||||
if serializer_class is not None:
|
if serializer_class is not None:
|
||||||
return serializer_class
|
return serializer_class
|
||||||
|
|
||||||
# TODO: Deprecation warning
|
|
||||||
class DefaultSerializer(self.model_serializer_class):
|
class DefaultSerializer(self.model_serializer_class):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = self.model
|
model = self.model
|
||||||
|
@ -184,7 +200,6 @@ class GenericAPIView(views.APIView):
|
||||||
return self.queryset._clone()
|
return self.queryset._clone()
|
||||||
|
|
||||||
if self.model is not None:
|
if self.model is not None:
|
||||||
# TODO: Deprecation warning
|
|
||||||
return self.model._default_manager.all()
|
return self.model._default_manager.all()
|
||||||
|
|
||||||
raise ImproperlyConfigured("'%s' must define 'queryset' or 'model'"
|
raise ImproperlyConfigured("'%s' must define 'queryset' or 'model'"
|
||||||
|
|
|
@ -76,12 +76,9 @@ class ListModelMixin(object):
|
||||||
error_msg = self.empty_error % {'class_name': class_name}
|
error_msg = self.empty_error % {'class_name': class_name}
|
||||||
raise Http404(error_msg)
|
raise Http404(error_msg)
|
||||||
|
|
||||||
# Pagination size is set by the `.paginate_by` attribute,
|
# Switch between paginated or standard style responses
|
||||||
# which may be `None` to disable pagination.
|
page = self.paginate_queryset(self.object_list)
|
||||||
page_size = self.get_paginate_by()
|
if page is not None:
|
||||||
if page_size:
|
|
||||||
packed = self.paginate_queryset(self.object_list, page_size)
|
|
||||||
paginator, page, queryset, is_paginated = packed
|
|
||||||
serializer = self.get_pagination_serializer(page)
|
serializer = self.get_pagination_serializer(page)
|
||||||
else:
|
else:
|
||||||
serializer = self.get_serializer(self.object_list, many=True)
|
serializer = self.get_serializer(self.object_list, many=True)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user