Created
January 11, 2018 02:52
-
-
Save kbrownlees/0c615eaea66802daeb924897abf67cdd to your computer and use it in GitHub Desktop.
django rest framework LimitOffsetPaginationNoCount
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
from collections import OrderedDict | |
from rest_framework.pagination import LimitOffsetPagination | |
class LimitOffsetPaginationNoCount(LimitOffsetPagination): | |
def paginate_queryset(self, queryset, request, view=None): | |
self.limit = self.get_limit(request) | |
if self.limit is None: | |
return None | |
self.offset = self.get_offset(request) | |
self.request = request | |
data = list(queryset[self.offset:self.offset + self.limit + 1]) | |
self.more = len(data) == (self.limit + 1) | |
if self.more: | |
data.pop() | |
self.count = self.offset + self.limit + 1 | |
else: | |
self.count = self.offset + len(data) | |
if self.more and self.template is not None: | |
self.display_page_controls = True | |
return data | |
def get_paginated_response(self, data): | |
return Response(OrderedDict([ | |
('next', self.get_next_link()), | |
('previous', self.get_previous_link()), | |
('results', data) | |
])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment