From 3a8e1d82abc26c77fac01c87038c448f5a6660bb Mon Sep 17 00:00:00 2001 From: Charles McBride Date: Thu, 22 Feb 2018 18:08:48 -0500 Subject: [PATCH 1/2] Add LimitOffsetPagination.get_count to allow method override --- rest_framework/pagination.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 4a74b6ed5f..e9c4560d6c 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -45,16 +45,6 @@ def _divide_with_ceil(a, b): return a // b -def _get_count(queryset): - """ - Determine an object count, supporting either querysets or regular lists. - """ - try: - return queryset.count() - except (AttributeError, TypeError): - return len(queryset) - - def _get_displayed_page_numbers(current, final): """ This utility function determines a list of page numbers to display. @@ -332,7 +322,7 @@ class LimitOffsetPagination(BasePagination): template = 'rest_framework/pagination/numbers.html' def paginate_queryset(self, queryset, request, view=None): - self.count = _get_count(queryset) + self.count = self.get_count(queryset) self.limit = self.get_limit(request) if self.limit is None: return None @@ -468,6 +458,15 @@ def get_schema_fields(self, view): ) ] + def get_count(self, queryset): + """ + Determine an object count, supporting either querysets or regular lists. + """ + try: + return queryset.count() + except (AttributeError, TypeError): + return len(queryset) + class CursorPagination(BasePagination): """ From 0a2fd620531b7e78e3490dd597f10fa8b34d7c2f Mon Sep 17 00:00:00 2001 From: Charles McBride Date: Mon, 26 Feb 2018 15:41:36 -0500 Subject: [PATCH 2/2] Format method docstring --- rest_framework/pagination.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index e9c4560d6c..1c57f17d31 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -460,8 +460,8 @@ def get_schema_fields(self, view): def get_count(self, queryset): """ - Determine an object count, supporting either querysets or regular lists. - """ + Determine an object count, supporting either querysets or regular lists. + """ try: return queryset.count() except (AttributeError, TypeError):