mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 01:26:53 +03:00
Make DEFAULT_PAGINATION_CLASS
None
by default. (#5170)
* 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 * DRF-5168 import warnings missed this in last commit * Add a system checks file Add a check for pagination settings for the 3.7 upgrade cycle. * more compatible import approach * missing bactic * revised language and approach to import the system check Adds a rest framework app config. * Adjust doc wording
This commit is contained in:
parent
11e5851196
commit
107e8b3d23
|
@ -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. Both `DEFAULT_PAGINATION_CLASS` and `PAGE_SIZE` are `None` 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',
|
||||||
|
|
|
@ -21,3 +21,5 @@ HTTP_HEADER_ENCODING = 'iso-8859-1'
|
||||||
|
|
||||||
# Default datetime input and output formats
|
# Default datetime input and output formats
|
||||||
ISO_8601 = 'iso-8601'
|
ISO_8601 = 'iso-8601'
|
||||||
|
|
||||||
|
default_app_config = 'rest_framework.apps.RestFrameworkConfig'
|
||||||
|
|
10
rest_framework/apps.py
Normal file
10
rest_framework/apps.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class RestFrameworkConfig(AppConfig):
|
||||||
|
name = 'rest_framework'
|
||||||
|
verbose_name = "Django REST framework"
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
# Add System checks
|
||||||
|
from .checks import pagination_system_check # NOQA
|
18
rest_framework/checks.py
Normal file
18
rest_framework/checks.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from django.core.checks import Tags, Warning, register
|
||||||
|
|
||||||
|
|
||||||
|
@register(Tags.compatibility)
|
||||||
|
def pagination_system_check(app_configs, **kwargs):
|
||||||
|
errors = []
|
||||||
|
# Use of default page size setting requires a default Paginator class
|
||||||
|
from rest_framework.settings import api_settings
|
||||||
|
if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS:
|
||||||
|
errors.append(
|
||||||
|
Warning(
|
||||||
|
"You have specified a default PAGE_SIZE pagination rest_framework setting,"
|
||||||
|
"without specifying also a DEFAULT_PAGINATION_CLASS.",
|
||||||
|
hint="The default for DEFAULT_PAGINATION_CLASS is None. "
|
||||||
|
"In previous versions this was PageNumberPagination",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return errors
|
|
@ -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