Last active
August 29, 2015 14:01
-
-
Save ph87/c022d14c59494af1289f to your computer and use it in GitHub Desktop.
override related django rest framework's generics pagination for only get the count of result by passing param page_size = 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read the codes commented by ph87 | |
def paginate_queryset(self, queryset, page_size=None): | |
""" | |
Paginate a queryset if required, either returning a page object, | |
or `None` if pagination is not configured for this view. | |
""" | |
deprecated_style = False | |
if page_size is not None: | |
warnings.warn('The `page_size` parameter to `paginate_queryset()` ' | |
'is due to be deprecated. ' | |
'Note that the return style of this method is also ' | |
'changed, and will simply return a page object ' | |
'when called without a `page_size` argument.', | |
PendingDeprecationWarning, stacklevel=2) | |
deprecated_style = True | |
else: | |
# Determine the required page size. | |
# If pagination is not configured, simply return None. | |
page_size = self.get_paginate_by() | |
# ph87 | |
if page_size == 0: | |
from django.core.paginator import Page | |
class Paginator(): | |
def __init__(self, queryset): | |
self.count = queryset.count() | |
self.num_pages = 0 | |
def validate_number(self, number): | |
return number | |
paginator = Paginator(queryset) | |
page = Page(None, 0, paginator) | |
return page | |
# ph87 | |
if not page_size: | |
return None | |
if not self.allow_empty: | |
warnings.warn( | |
'The `allow_empty` parameter is due to be deprecated. ' | |
'To use `allow_empty=False` style behavior, You should override ' | |
'`get_queryset()` and explicitly raise a 404 on empty querysets.', | |
PendingDeprecationWarning, stacklevel=2 | |
) | |
paginator = self.paginator_class(queryset, page_size, | |
allow_empty_first_page=self.allow_empty) | |
page_kwarg = self.kwargs.get(self.page_kwarg) | |
page_query_param = self.request.QUERY_PARAMS.get(self.page_kwarg) | |
page = page_kwarg or page_query_param or 1 | |
try: | |
page_number = paginator.validate_number(page) | |
except InvalidPage: | |
if page == 'last': | |
page_number = paginator.num_pages | |
else: | |
raise Http404(_("Page is not 'last', nor can it be converted to an int.")) | |
try: | |
page = paginator.page(page_number) | |
except InvalidPage as e: | |
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % { | |
'page_number': page_number, | |
'message': str(e) | |
}) | |
if deprecated_style: | |
return (paginator, page, page.object_list, page.has_other_pages()) | |
return page | |
######################## | |
### The following methods provide default implementations | |
### that you may want to override for more complex cases. | |
def get_paginate_by(self, queryset=None): | |
""" | |
Return the size of pages to use with pagination. | |
If `PAGINATE_BY_PARAM` is set it will attempt to get the page size | |
from a named query parameter in the url, eg. ?page_size=100 | |
Otherwise defaults to using `self.paginate_by`. | |
""" | |
if queryset is not None: | |
warnings.warn('The `queryset` parameter to `get_paginate_by()` ' | |
'is due to be deprecated.', | |
PendingDeprecationWarning, stacklevel=2) | |
if self.paginate_by_param: | |
try: | |
# ph87 | |
if self.request.QUERY_PARAMS[self.paginate_by_param] == '0': | |
return 0 | |
# ph87 | |
return strict_positive_int( | |
self.request.QUERY_PARAMS[self.paginate_by_param], | |
cutoff=self.max_paginate_by | |
) | |
except (KeyError, ValueError): | |
pass | |
return self.paginate_by | |
GenericAPIView.paginate_queryset = paginate_queryset | |
GenericAPIView.get_paginate_by = get_paginate_by |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment