Skip to content

Include correct limits in LimitOffsetPagination link urls #3015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rest_framework/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ def get_next_link(self):
return None

url = self.request.build_absolute_uri()
url = replace_query_param(url, self.limit_query_param, self.limit)

offset = self.offset + self.limit
return replace_query_param(url, self.offset_query_param, offset)

Expand All @@ -439,6 +441,7 @@ def get_previous_link(self):
return None

url = self.request.build_absolute_uri()
url = replace_query_param(url, self.limit_query_param, self.limit)

if self.offset - self.limit <= 0:
return remove_query_param(url, self.offset_query_param)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ class TestLimitOffset:
def setup(self):
class ExamplePagination(pagination.LimitOffsetPagination):
default_limit = 10
max_limit = 15

self.pagination = ExamplePagination()
self.queryset = range(1, 101)

Expand Down Expand Up @@ -442,7 +444,31 @@ def test_invalid_limit(self):
"""
request = Request(factory.get('/', {'limit': 'invalid', 'offset': 0}))
queryset = self.paginate_queryset(request)
content = self.get_paginated_content(queryset)
next_limit = self.pagination.default_limit
next_offset = self.pagination.default_limit
next_url = 'http://testserver/?limit={0}&offset={1}'.format(next_limit, next_offset)
assert queryset == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
assert content.get('next') == next_url

def test_max_limit(self):
"""
The limit defaults to the max_limit when there is a max_limit and the
requested limit is greater than the max_limit
"""
offset = 50
request = Request(factory.get('/', {'limit': '11235', 'offset': offset}))
queryset = self.paginate_queryset(request)
content = self.get_paginated_content(queryset)
max_limit = self.pagination.max_limit
next_offset = offset + max_limit
prev_offset = offset - max_limit
base_url = 'http://testserver/?limit={0}'.format(max_limit)
next_url = base_url + '&offset={0}'.format(next_offset)
prev_url = base_url + '&offset={0}'.format(prev_offset)
assert queryset == list(range(51, 66))
assert content.get('next') == next_url
assert content.get('previous') == prev_url


class TestCursorPagination:
Expand Down