diff options
| author | Tom Christie | 2015-01-13 17:14:13 +0000 |
|---|---|---|
| committer | Tom Christie | 2015-01-13 17:14:13 +0000 |
| commit | 1bcec3a0ac4346b31b655a08505d3e3dc2156604 (patch) | |
| tree | 321a606d180c1edbb0c56c5f98e5db5ddcbe1bd8 /rest_framework | |
| parent | 564f845e21cd55669311db9491b85dc86a5ff628 (diff) | |
| download | django-rest-framework-1bcec3a0ac4346b31b655a08505d3e3dc2156604.tar.bz2 | |
API tweaks and pagination documentation
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/generics.py | 6 | ||||
| -rw-r--r-- | rest_framework/pagination.py | 28 |
2 files changed, 22 insertions, 12 deletions
diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 12fb6413..cdf6ece0 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -160,11 +160,11 @@ class GenericAPIView(views.APIView): def paginate_queryset(self, queryset): if self.pager is None: - return None + return queryset return self.pager.paginate_queryset(queryset, self.request, view=self) - def get_paginated_response(self, objects): - return self.pager.get_paginated_response(objects) + def get_paginated_response(self, data): + return self.pager.get_paginated_response(data) # Concrete view classes that provide method handlers 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): |
