Skip to content

Commit a9d25af

Browse files
Add MAX_PAGE_SIZE setting
1 parent f113ab6 commit a9d25af

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

docs/api-guide/pagination.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ Pagination can be turned off by setting the pagination class to `None`.
2424

2525
## Setting the pagination style
2626

27-
The pagination style may be set globally, using the `DEFAULT_PAGINATION_CLASS` and `PAGE_SIZE` setting keys. For example, to use the built-in limit/offset pagination, you would do something like this:
27+
The pagination style may be set globally, using the `DEFAULT_PAGINATION_CLASS`, `PAGE_SIZE` and `MAX_PAGE_SIZE` setting keys. For example, to use the built-in limit/offset pagination, you would do something like this:
2828

2929
REST_FRAMEWORK = {
3030
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
31-
'PAGE_SIZE': 100
31+
'PAGE_SIZE': 100,
32+
'MAX_PAGE_SIZE': 250,
3233
}
3334

34-
Note that you need to set both the pagination class, and the page size that should be used. Both `DEFAULT_PAGINATION_CLASS` and `PAGE_SIZE` are `None` by default.
35+
Note that you need to set both the pagination class, and the page size and limit that should be used.
36+
`DEFAULT_PAGINATION_CLASS`, `PAGE_SIZE`, `MAX_PAGE_SIZE` are `None` by default.
3537

3638
You can also set the pagination class on an individual view by using the `pagination_class` attribute. Typically you'll want to use the same pagination style throughout your API, although you might want to vary individual aspects of the pagination, such as default or maximum page size, on a per-view basis.
3739

rest_framework/pagination.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ class PageNumberPagination(BasePagination):
172172
# The default page size.
173173
# Defaults to `None`, meaning pagination is disabled.
174174
page_size = api_settings.PAGE_SIZE
175+
# The maximum page size.
176+
# Defaults to `None`, meaning page size is unlimited.
177+
# It's recommended that you would set a limit to avoid api abuse.
178+
page_size = api_settings.MAX_PAGE_SIZE
175179

176180
django_paginator_class = DjangoPaginator
177181

@@ -382,7 +386,7 @@ class LimitOffsetPagination(BasePagination):
382386
limit_query_description = _('Number of results to return per page.')
383387
offset_query_param = 'offset'
384388
offset_query_description = _('The initial index from which to return the results.')
385-
max_limit = None
389+
max_limit = api_settings.MAX_PAGE_SIZE
386390
template = 'rest_framework/pagination/numbers.html'
387391

388392
def paginate_queryset(self, queryset, request, view=None):

rest_framework/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
# Pagination
6767
'PAGE_SIZE': None,
68+
"MAX_PAGE_SIZE": None,
6869

6970
# Filtering
7071
'SEARCH_PARAM': 'search',

0 commit comments

Comments
 (0)