mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Changes to the paginator defaults and settings
Require a default paginator be specified when using the page size setting. https://github.com/encode/django-rest-framework/issues/5168
This commit is contained in:
parent
99569190ab
commit
e52d31d09b
|
@ -21,14 +21,14 @@ Pagination can be turned off by setting the pagination class to `None`.
|
||||||
|
|
||||||
## Setting the pagination style
|
## Setting the pagination style
|
||||||
|
|
||||||
The default 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:
|
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:
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
|
||||||
'PAGE_SIZE': 100
|
'PAGE_SIZE': 100
|
||||||
}
|
}
|
||||||
|
|
||||||
Note that you need to set both the pagination class, and the page size that should be used.
|
Note that you need to set both the pagination class, and the page size that should be used. The `DEFAULT_PAGINATION_CLASS` is set to `None` and disabled by default.
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ This pagination style accepts a single number page number in the request query p
|
||||||
|
|
||||||
#### Setup
|
#### Setup
|
||||||
|
|
||||||
To enable the `PageNumberPagination` style globally, use the following configuration, modifying the `PAGE_SIZE` as desired:
|
To enable the `PageNumberPagination` style globally, use the following configuration, and set the `PAGE_SIZE` as desired:
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
||||||
|
|
|
@ -146,6 +146,17 @@ PAGE_BREAK = PageLink(url=None, number=None, is_active=False, is_break=True)
|
||||||
class BasePagination(object):
|
class BasePagination(object):
|
||||||
display_page_controls = False
|
display_page_controls = False
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
# Use of default page size setting requires a default Paginator class
|
||||||
|
if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS:
|
||||||
|
warnings.warn(
|
||||||
|
"A valid paginator class must be specified with `DEFAULT_PAGINATION_CLASS` "
|
||||||
|
"when using the `PAGE_SIZE` default pagination setting."
|
||||||
|
"Defaulting the setting to specifies `PageNumberPagination` "
|
||||||
|
"is deprecated as pagination is disabled by default.",
|
||||||
|
DeprecationWarning
|
||||||
|
)
|
||||||
|
|
||||||
def paginate_queryset(self, queryset, request, view=None): # pragma: no cover
|
def paginate_queryset(self, queryset, request, view=None): # pragma: no cover
|
||||||
raise NotImplementedError('paginate_queryset() must be implemented.')
|
raise NotImplementedError('paginate_queryset() must be implemented.')
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ DEFAULTS = {
|
||||||
'DEFAULT_VERSIONING_CLASS': None,
|
'DEFAULT_VERSIONING_CLASS': None,
|
||||||
|
|
||||||
# Generic view behavior
|
# Generic view behavior
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
'DEFAULT_PAGINATION_CLASS': None,
|
||||||
'DEFAULT_FILTER_BACKENDS': (),
|
'DEFAULT_FILTER_BACKENDS': (),
|
||||||
|
|
||||||
# Throttling
|
# Throttling
|
||||||
|
|
Loading…
Reference in New Issue
Block a user