mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Tweaks
This commit is contained in:
parent
3833a5bb8a
commit
313aa727e3
|
@ -74,7 +74,7 @@ Note that the `paginate_queryset` method may set state on the pagination instanc
|
||||||
|
|
||||||
Let's modify the built-in `PageNumberPagination` style, so that instead of include the pagination links in the body of the response, we'll instead include a `Link` header, in a [similar style to the GitHub API][github-link-pagination].
|
Let's modify the built-in `PageNumberPagination` style, so that instead of include the pagination links in the body of the response, we'll instead include a `Link` header, in a [similar style to the GitHub API][github-link-pagination].
|
||||||
|
|
||||||
class LinkHeaderPagination(PageNumberPagination)
|
class LinkHeaderPagination(pagination.PageNumberPagination):
|
||||||
def get_paginated_response(self, data):
|
def get_paginated_response(self, data):
|
||||||
next_url = self.get_next_link()
previous_url = self.get_previous_link()
|
next_url = self.get_next_link()
previous_url = self.get_previous_link()
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ Let's modify the built-in `PageNumberPagination` style, so that instead of inclu
|
||||||
link = '<{next_url}; rel="next">, <{previous_url}; rel="prev">'
|
link = '<{next_url}; rel="next">, <{previous_url}; rel="prev">'
|
||||||
elif next_url is not None:
|
elif next_url is not None:
|
||||||
link = '<{next_url}; rel="next">'
|
link = '<{next_url}; rel="next">'
|
||||||
elif prev_url is not None:
|
elif previous_url is not None:
|
||||||
link = '<{previous_url}; rel="prev">'
|
link = '<{previous_url}; rel="prev">'
|
||||||
else:
|
else:
|
||||||
link = ''
|
link = ''
|
||||||
|
@ -97,10 +97,20 @@ Let's modify the built-in `PageNumberPagination` style, so that instead of inclu
|
||||||
To have your custom pagination class be used by default, use the `DEFAULT_PAGINATION_CLASS` setting:
|
To have your custom pagination class be used by default, use the `DEFAULT_PAGINATION_CLASS` setting:
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_PAGINATION_CLASS':
|
'DEFAULT_PAGINATION_CLASS': 'my_project.apps.core.pagination.LinkHeaderPagination',
|
||||||
'my_project.apps.core.pagination.LinkHeaderPagination',
|
'PAGINATE_BY': 10
|
||||||
}
|
}
|
||||||
|
|
||||||
|
API responses for list endpoints will now include a `Link` header, instead of including the pagination links as part of the body of the response, for example:
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
![Link Header][link-header]
|
||||||
|
|
||||||
|
*A custom pagination style, using the 'Link' header'*
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
# Third party packages
|
# Third party packages
|
||||||
|
|
||||||
The following third party packages are also available.
|
The following third party packages are also available.
|
||||||
|
@ -111,5 +121,6 @@ The [`DRF-extensions` package][drf-extensions] includes a [`PaginateByMaxMixin`
|
||||||
|
|
||||||
[cite]: https://docs.djangoproject.com/en/dev/topics/pagination/
|
[cite]: https://docs.djangoproject.com/en/dev/topics/pagination/
|
||||||
[github-link-pagination]: https://developer.github.com/guides/traversing-with-pagination/
|
[github-link-pagination]: https://developer.github.com/guides/traversing-with-pagination/
|
||||||
|
[link-header]: ../img/link-header-pagination.png
|
||||||
[drf-extensions]: http://chibisov.github.io/drf-extensions/docs/
|
[drf-extensions]: http://chibisov.github.io/drf-extensions/docs/
|
||||||
[paginate-by-max-mixin]: http://chibisov.github.io/drf-extensions/docs/#paginatebymaxmixin
|
[paginate-by-max-mixin]: http://chibisov.github.io/drf-extensions/docs/#paginatebymaxmixin
|
||||||
|
|
BIN
docs/img/link-header-pagination.png
Normal file
BIN
docs/img/link-header-pagination.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
|
@ -46,6 +46,12 @@ def _get_displayed_page_numbers(current, final):
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
current=14, final=16 -> [1, None, 13, 14, 15, 16]
|
current=14, final=16 -> [1, None, 13, 14, 15, 16]
|
||||||
|
|
||||||
|
This implementation gives one page to each side of the cursor,
|
||||||
|
for an implementation which gives two pages to each side of the cursor,
|
||||||
|
which is a copy of how GitHub treat pagination in their issue lists, see:
|
||||||
|
|
||||||
|
https://gist.github.com/tomchristie/321140cebb1c4a558b15
|
||||||
"""
|
"""
|
||||||
assert current >= 1
|
assert current >= 1
|
||||||
assert final >= current
|
assert final >= current
|
||||||
|
@ -60,10 +66,12 @@ def _get_displayed_page_numbers(current, final):
|
||||||
|
|
||||||
# If the break would only exclude a single page number then we
|
# If the break would only exclude a single page number then we
|
||||||
# may as well include the page number instead of the break.
|
# may as well include the page number instead of the break.
|
||||||
if current == 4:
|
if current <= 4:
|
||||||
included.add(2)
|
included.add(2)
|
||||||
if current == final - 3:
|
included.add(3)
|
||||||
|
if current >= final - 3:
|
||||||
included.add(final - 1)
|
included.add(final - 1)
|
||||||
|
included.add(final - 2)
|
||||||
|
|
||||||
# Now sort the page numbers and drop anything outside the limits.
|
# Now sort the page numbers and drop anything outside the limits.
|
||||||
included = [
|
included = [
|
||||||
|
|
Loading…
Reference in New Issue
Block a user