django-rest-framework/docs/api-guide/settings.md

260 lines
7.8 KiB
Markdown
Raw Normal View History

2012-09-09 01:06:13 +04:00
<a class="github" href="settings.py"></a>
2012-09-05 13:01:43 +04:00
# Settings
2012-09-12 13:12:13 +04:00
> Namespaces are one honking great idea - let's do more of those!
>
> &mdash; [The Zen of Python][cite]
2012-09-06 00:14:00 +04:00
Configuration for REST framework is all namespaced inside a single Django setting, named `REST_FRAMEWORK`.
2012-09-12 13:12:13 +04:00
For example your project's `settings.py` file might include something like this:
2012-09-05 13:01:43 +04:00
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.YAMLRenderer',
2012-10-31 13:41:56 +04:00
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.YAMLParser',
2012-09-05 13:01:43 +04:00
)
}
2012-09-06 19:46:56 +04:00
## Accessing settings
If you need to access the values of REST framework's API settings in your project,
you should use the `api_settings` object. For example.
from rest_framework.settings import api_settings
2012-09-06 19:46:56 +04:00
print api_settings.DEFAULT_AUTHENTICATION_CLASSES
2012-09-06 19:46:56 +04:00
The `api_settings` object will check for any user-defined settings, and otherwise fallback to the default values. Any setting that uses string import paths to refer to a class will automatically import and return the referenced class, instead of the string literal.
2012-10-17 18:41:57 +04:00
---
# API Reference
2013-03-06 16:19:39 +04:00
## API policy settings
*The following settings control the basic API policies, and are applied to every `APIView` class based view, or `@api_view` function based view.*
#### DEFAULT_RENDERER_CLASSES
2012-09-05 13:01:43 +04:00
2012-09-05 16:03:55 +04:00
A list or tuple of renderer classes, that determines the default set of renderers that may be used when returning a `Response` object.
2012-09-05 13:01:43 +04:00
Default:
(
'rest_framework.renderers.JSONRenderer',
2012-10-29 12:13:56 +04:00
'rest_framework.renderers.BrowsableAPIRenderer',
2012-09-05 16:03:55 +04:00
)
2012-09-05 13:01:43 +04:00
2013-03-06 16:19:39 +04:00
#### DEFAULT_PARSER_CLASSES
2012-09-05 13:01:43 +04:00
2012-09-05 16:03:55 +04:00
A list or tuple of parser classes, that determines the default set of parsers used when accessing the `request.DATA` property.
Default:
(
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
2012-09-05 16:03:55 +04:00
)
2013-03-06 16:19:39 +04:00
#### DEFAULT_AUTHENTICATION_CLASSES
2012-09-05 16:03:55 +04:00
A list or tuple of authentication classes, that determines the default set of authenticators used when accessing the `request.user` or `request.auth` properties.
2012-09-06 18:57:16 +04:00
Default:
2012-09-05 16:03:55 +04:00
(
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication'
2012-09-05 16:03:55 +04:00
)
2013-03-06 16:19:39 +04:00
#### DEFAULT_PERMISSION_CLASSES
2012-09-05 16:03:55 +04:00
2012-09-06 00:14:00 +04:00
A list or tuple of permission classes, that determines the default set of permissions checked at the start of a view.
2012-10-27 23:04:33 +04:00
Default:
(
'rest_framework.permissions.AllowAny',
)
2012-09-05 16:03:55 +04:00
2013-03-06 16:19:39 +04:00
#### DEFAULT_THROTTLE_CLASSES
2012-09-05 13:01:43 +04:00
2012-09-06 00:14:00 +04:00
A list or tuple of throttle classes, that determines the default set of throttles checked at the start of a view.
2012-09-05 13:01:43 +04:00
Default: `()`
2012-09-05 16:03:55 +04:00
2013-03-06 16:19:39 +04:00
#### DEFAULT_CONTENT_NEGOTIATION_CLASS
A content negotiation class, that determines how a renderer is selected for the response, given an incoming request.
Default: `'rest_framework.negotiation.DefaultContentNegotiation'`
2013-03-06 16:19:39 +04:00
---
## Generic view settings
*The following settings control the behavior of the generic class based views.*
#### DEFAULT_MODEL_SERIALIZER_CLASS
2012-09-05 16:03:55 +04:00
A class that determines the default type of model serializer that should be used by a generic view if `model` is specified, but `serializer_class` is not provided.
2012-09-06 19:46:56 +04:00
Default: `'rest_framework.serializers.ModelSerializer'`
2012-09-05 16:03:55 +04:00
2013-03-06 16:19:39 +04:00
#### DEFAULT_PAGINATION_SERIALIZER_CLASS
2012-09-05 16:03:55 +04:00
A class the determines the default serialization style for paginated responses.
2012-09-06 19:46:56 +04:00
Default: `rest_framework.pagination.PaginationSerializer`
2012-09-05 16:03:55 +04:00
2013-05-07 16:00:44 +04:00
#### DEFAULT_FILTER_BACKENDS
2012-09-05 16:03:55 +04:00
2013-05-07 16:00:44 +04:00
A list of filter backend classes that should be used for generic filtering.
If set to `None` then generic filtering is disabled.
2012-09-06 19:46:56 +04:00
2013-03-06 16:19:39 +04:00
#### PAGINATE_BY
The default page size to use for pagination. If set to `None`, pagination is disabled by default.
Default: `None`
2013-03-06 16:19:39 +04:00
#### PAGINATE_BY_PARAM
2013-05-28 18:09:23 +04:00
The name of a query parameter, which can be used by the client to override the default page size to use for pagination. If set to `None`, clients may not override the default page size.
Default: `None`
2012-09-05 16:03:55 +04:00
2013-03-06 16:19:39 +04:00
---
## Authentication settings
*The following settings control the behavior of unauthenticated requests.*
#### UNAUTHENTICATED_USER
2012-09-05 23:10:06 +04:00
The class that should be used to initialize `request.user` for unauthenticated requests.
2012-09-05 16:03:55 +04:00
Default: `django.contrib.auth.models.AnonymousUser`
2013-03-06 16:19:39 +04:00
#### UNAUTHENTICATED_TOKEN
2012-09-05 23:10:06 +04:00
The class that should be used to initialize `request.auth` for unauthenticated requests.
Default: `None`
2013-03-06 16:19:39 +04:00
---
## Browser overrides
*The following settings provide URL or form-based overrides of the default browser behavior.*
#### FORM_METHOD_OVERRIDE
2012-09-05 16:03:55 +04:00
2012-09-05 23:10:06 +04:00
The name of a form field that may be used to override the HTTP method of the form.
2012-09-06 00:14:00 +04:00
If the value of this setting is `None` then form method overloading will be disabled.
2012-09-05 23:10:06 +04:00
Default: `'_method'`
2012-09-05 16:03:55 +04:00
2013-03-06 16:19:39 +04:00
#### FORM_CONTENT_OVERRIDE
2012-09-05 16:03:55 +04:00
2012-09-06 00:14:00 +04:00
The name of a form field that may be used to override the content of the form payload. Must be used together with `FORM_CONTENTTYPE_OVERRIDE`.
If either setting is `None` then form content overloading will be disabled.
2012-09-05 23:10:06 +04:00
Default: `'_content'`
2012-09-05 16:03:55 +04:00
2013-03-06 16:19:39 +04:00
#### FORM_CONTENTTYPE_OVERRIDE
2012-09-05 16:03:55 +04:00
2012-09-06 00:14:00 +04:00
The name of a form field that may be used to override the content type of the form payload. Must be used together with `FORM_CONTENT_OVERRIDE`.
If either setting is `None` then form content overloading will be disabled.
2012-09-05 23:10:06 +04:00
Default: `'_content_type'`
2012-09-05 16:03:55 +04:00
2013-03-06 16:19:39 +04:00
#### URL_ACCEPT_OVERRIDE
2012-09-05 16:03:55 +04:00
2012-09-05 23:10:06 +04:00
The name of a URL parameter that may be used to override the HTTP `Accept` header.
2012-09-06 00:14:00 +04:00
If the value of this setting is `None` then URL accept overloading will be disabled.
Default: `'accept'`
2013-03-06 16:19:39 +04:00
#### URL_FORMAT_OVERRIDE
The name of a URL parameter that may be used to override the default `Accept` header based content negotiation.
Default: `'format'`
2012-09-12 13:12:13 +04:00
2013-03-06 16:19:39 +04:00
---
## Date and time formatting
2013-03-06 16:19:39 +04:00
*The following settings are used to control how date and time representations may be parsed and rendered.*
2013-03-06 16:19:39 +04:00
#### DATETIME_FORMAT
2013-03-01 18:03:27 +04:00
A format string that should be used by default for rendering the output of `DateTimeField` serializer fields. If `None`, then `DateTimeField` serializer fields will return python `datetime` objects, and the datetime encoding will be determined by the renderer.
2013-03-01 18:03:27 +04:00
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
Default: `None`
2013-03-01 19:35:27 +04:00
2013-03-06 16:19:39 +04:00
#### DATETIME_INPUT_FORMATS
2013-03-01 18:03:27 +04:00
2013-03-06 00:57:35 +04:00
A list of format strings that should be used by default for parsing inputs to `DateTimeField` serializer fields.
2013-03-01 18:03:27 +04:00
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
2013-03-06 16:19:39 +04:00
Default: `['iso-8601']`
2013-03-01 18:03:27 +04:00
2013-03-06 16:19:39 +04:00
#### DATE_FORMAT
2013-03-01 19:35:27 +04:00
A format string that should be used by default for rendering the output of `DateField` serializer fields. If `None`, then `DateField` serializer fields will return python `date` objects, and the date encoding will be determined by the renderer.
2013-03-06 00:57:35 +04:00
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
Default: `None`
2013-03-06 00:57:35 +04:00
2013-03-06 16:19:39 +04:00
#### DATE_INPUT_FORMATS
2013-03-01 18:03:27 +04:00
2013-03-06 00:57:35 +04:00
A list of format strings that should be used by default for parsing inputs to `DateField` serializer fields.
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
2013-03-06 16:19:39 +04:00
Default: `['iso-8601']`
2013-03-06 00:57:35 +04:00
2013-03-06 16:19:39 +04:00
#### TIME_FORMAT
2013-03-06 00:57:35 +04:00
A format string that should be used by default for rendering the output of `TimeField` serializer fields. If `None`, then `TimeField` serializer fields will return python `time` objects, and the time encoding will be determined by the renderer.
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
2013-03-06 00:57:35 +04:00
Default: `None`
2013-03-06 00:57:35 +04:00
2013-03-06 16:19:39 +04:00
#### TIME_INPUT_FORMATS
2013-03-01 18:03:27 +04:00
2013-03-06 00:57:35 +04:00
A list of format strings that should be used by default for parsing inputs to `TimeField` serializer fields.
2013-03-01 18:03:27 +04:00
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
2013-03-06 16:19:39 +04:00
Default: `['iso-8601']`
---
## Miscellaneous settings
#### FORMAT_SUFFIX_KWARG
The name of a parameter in the URL conf that may be used to provide a format suffix.
Default: `'format'`
2013-03-01 19:35:27 +04:00
2012-09-12 13:12:13 +04:00
[cite]: http://www.python.org/dev/peps/pep-0020/
2013-05-07 16:00:44 +04:00
[strftime]: http://docs.python.org/2/library/time.html#time.strftime