aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/pagination.py
diff options
context:
space:
mode:
authorTom Christie2015-01-13 17:14:13 +0000
committerTom Christie2015-01-13 17:14:13 +0000
commit1bcec3a0ac4346b31b655a08505d3e3dc2156604 (patch)
tree321a606d180c1edbb0c56c5f98e5db5ddcbe1bd8 /rest_framework/pagination.py
parent564f845e21cd55669311db9491b85dc86a5ff628 (diff)
downloaddjango-rest-framework-1bcec3a0ac4346b31b655a08505d3e3dc2156604.tar.bz2
API tweaks and pagination documentation
Diffstat (limited to 'rest_framework/pagination.py')
-rw-r--r--rest_framework/pagination.py28
1 files changed, 19 insertions, 9 deletions
diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py
index da2d60a4..b9d48796 100644
--- a/rest_framework/pagination.py
+++ b/rest_framework/pagination.py
@@ -25,11 +25,21 @@ def _strict_positive_int(integer_string, cutoff=None):
return ret
+def _get_count(queryset):
+ """
+ Determine an object count, supporting either querysets or regular lists.
+ """
+ try:
+ return queryset.count()
+ except AttributeError:
+ return len(queryset)
+
+
class BasePagination(object):
- def paginate_queryset(self, queryset, request):
+ def paginate_queryset(self, queryset, request, view):
raise NotImplemented('paginate_queryset() must be implemented.')
- def get_paginated_response(self, data, page, request):
+ def get_paginated_response(self, data):
raise NotImplemented('get_paginated_response() must be implemented.')
@@ -58,8 +68,8 @@ class PageNumberPagination(BasePagination):
def paginate_queryset(self, queryset, request, view):
"""
- Paginate a queryset if required, either returning a page object,
- or `None` if pagination is not configured for this view.
+ Paginate a queryset if required, either returning a
+ page object, or `None` if pagination is not configured for this view.
"""
for attr in (
'paginate_by', 'page_query_param',
@@ -97,12 +107,12 @@ class PageNumberPagination(BasePagination):
self.request = request
return self.page
- def get_paginated_response(self, objects):
+ def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
- ('results', objects)
+ ('results', data)
]))
def get_page_size(self, request):
@@ -147,16 +157,16 @@ class LimitOffsetPagination(BasePagination):
def paginate_queryset(self, queryset, request, view):
self.limit = self.get_limit(request)
self.offset = self.get_offset(request)
- self.count = queryset.count()
+ self.count = _get_count(queryset)
self.request = request
return queryset[self.offset:self.offset + self.limit]
- def get_paginated_response(self, objects):
+ def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
- ('results', objects)
+ ('results', data)
]))
def get_limit(self, request):