mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
[ADD] New settings 'ORDERING'. [ADD] Allow define a 'page_size' by view when using 'PageNumberPagination'. [ADD] Allow define a 'limit' by view when using 'LimitOffsetPagination'. [ADD] Allow define a 'ordering' by view when using 'CursorPagination'.
This commit is contained in:
parent
1d2fba906e
commit
a723684c7f
|
@ -194,6 +194,7 @@ class PageNumberPagination(BasePagination):
|
||||||
Paginate a queryset if required, either returning a
|
Paginate a queryset if required, either returning a
|
||||||
page object, or `None` if pagination is not configured for this view.
|
page object, or `None` if pagination is not configured for this view.
|
||||||
"""
|
"""
|
||||||
|
self.view_page_size = getattr(view, 'page_size', None)
|
||||||
page_size = self.get_page_size(request)
|
page_size = self.get_page_size(request)
|
||||||
if not page_size:
|
if not page_size:
|
||||||
return None
|
return None
|
||||||
|
@ -237,7 +238,7 @@ class PageNumberPagination(BasePagination):
|
||||||
except (KeyError, ValueError):
|
except (KeyError, ValueError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return self.page_size
|
return self.view_page_size or self.page_size
|
||||||
|
|
||||||
def get_next_link(self):
|
def get_next_link(self):
|
||||||
if not self.page.has_next():
|
if not self.page.has_next():
|
||||||
|
@ -295,6 +296,7 @@ class LimitOffsetPagination(BasePagination):
|
||||||
template = 'rest_framework/pagination/numbers.html'
|
template = 'rest_framework/pagination/numbers.html'
|
||||||
|
|
||||||
def paginate_queryset(self, queryset, request, view=None):
|
def paginate_queryset(self, queryset, request, view=None):
|
||||||
|
self.view_limit = getattr(view, 'limit', None)
|
||||||
self.limit = self.get_limit(request)
|
self.limit = self.get_limit(request)
|
||||||
if self.limit is None:
|
if self.limit is None:
|
||||||
return None
|
return None
|
||||||
|
@ -325,7 +327,7 @@ class LimitOffsetPagination(BasePagination):
|
||||||
except (KeyError, ValueError):
|
except (KeyError, ValueError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return self.default_limit
|
return self.view_limit or self.default_limit
|
||||||
|
|
||||||
def get_offset(self, request):
|
def get_offset(self, request):
|
||||||
try:
|
try:
|
||||||
|
@ -414,7 +416,7 @@ class CursorPagination(BasePagination):
|
||||||
cursor_query_param = 'cursor'
|
cursor_query_param = 'cursor'
|
||||||
page_size = api_settings.PAGE_SIZE
|
page_size = api_settings.PAGE_SIZE
|
||||||
invalid_cursor_message = _('Invalid cursor')
|
invalid_cursor_message = _('Invalid cursor')
|
||||||
ordering = '-created'
|
ordering = api_settings.ORDERING
|
||||||
template = 'rest_framework/pagination/previous_and_next.html'
|
template = 'rest_framework/pagination/previous_and_next.html'
|
||||||
|
|
||||||
# The offset in the cursor is used in situations where we have a
|
# The offset in the cursor is used in situations where we have a
|
||||||
|
@ -429,6 +431,7 @@ class CursorPagination(BasePagination):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.base_url = request.build_absolute_uri()
|
self.base_url = request.build_absolute_uri()
|
||||||
|
self.view_ordering = getattr(view, 'ordering', None)
|
||||||
self.ordering = self.get_ordering(request, queryset, view)
|
self.ordering = self.get_ordering(request, queryset, view)
|
||||||
|
|
||||||
self.cursor = self.decode_cursor(request)
|
self.cursor = self.decode_cursor(request)
|
||||||
|
@ -623,7 +626,7 @@ class CursorPagination(BasePagination):
|
||||||
else:
|
else:
|
||||||
# The default case is to check for an `ordering` attribute
|
# The default case is to check for an `ordering` attribute
|
||||||
# on this pagination instance.
|
# on this pagination instance.
|
||||||
ordering = self.ordering
|
ordering = self.view_ordering or self.ordering
|
||||||
assert ordering is not None, (
|
assert ordering is not None, (
|
||||||
'Using cursor pagination, but no ordering attribute was declared '
|
'Using cursor pagination, but no ordering attribute was declared '
|
||||||
'on the pagination class.'
|
'on the pagination class.'
|
||||||
|
|
|
@ -64,6 +64,7 @@ DEFAULTS = {
|
||||||
|
|
||||||
# Pagination
|
# Pagination
|
||||||
'PAGE_SIZE': None,
|
'PAGE_SIZE': None,
|
||||||
|
'ORDERING': '-created',
|
||||||
|
|
||||||
# Filtering
|
# Filtering
|
||||||
'SEARCH_PARAM': 'search',
|
'SEARCH_PARAM': 'search',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user