Settings
DRF configuration is all namespaced inside a single Django setting named REST_FRAMEWORK.
Why Namespaced Settings?
NOTE
Namespaces are one honking great idea - let's do more of those!
— The Zen of Python
By keeping all configuration inside a single dictionary, DRF ensures that your project's settings.py remains clean and well-organized. This eliminates the need for dozens of individual prefixed settings, keeping your codebase DRY and manageable.
Global Configuration
To configure DRF globally, define a REST_FRAMEWORK dictionary in your settings.py. Any omitted settings will automatically fall back to DRF's default values.
# settings.py
REST_FRAMEWORK = {
# Authentication & Permissions
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
# Pagination
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100,
# Throttling
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
},
# Content Negotiation
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
],
# Filtering
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend'
],
# Date and Time Formats
'DATETIME_FORMAT': '%Y-%m-%dT%H:%M:%S.%fZ',
'DATE_FORMAT': 'iso-8601',
'TIME_FORMAT': 'iso-8601',
}Accessing Settings in Code
When writing custom DRF components, you may need to access these settings. Instead of reading directly from Django's settings.py, always use the api_settings object.
TIP
Why use api_settings? It automatically checks for user-defined settings in REST_FRAMEWORK, resolves import strings to actual classes and falls back to built-in defaults if a setting isn't explicitly defined.
# custom_code.py
from rest_framework.settings import api_settings
# This correctly falls back to defaults if omitted in settings.py
print(api_settings.DEFAULT_AUTHENTICATION_CLASSES)Key Settings & Defaults
DRF provides a wide range of settings. Below are some of the most commonly used, along with their default behaviors.
API Policy Settings
These settings dictate the fundamental behavior of every APIView and @api_view.
DEFAULT_RENDERER_CLASSESDetermines the default set of renderers for response formatting. Default:['rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer']DEFAULT_PARSER_CLASSESDetermines how incomingrequest.datais parsed. Default:['rest_framework.parsers.JSONParser', 'rest_framework.parsers.FormParser', 'rest_framework.parsers.MultiPartParser']DEFAULT_AUTHENTICATION_CLASSESDefines the authenticators used to populaterequest.user. Default:['rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication']DEFAULT_PERMISSION_CLASSESSets the default permission checks for views. Default:['rest_framework.permissions.AllowAny']DEFAULT_THROTTLE_CLASSESSets the default throttling policies. Default:[](Empty list)
Generic View Settings
These settings control the behavior of generic class-based views.
DEFAULT_PAGINATION_CLASSThe pagination style for querysets. Default:None(Pagination is disabled)PAGE_SIZEThe number of items per page if a pagination class is active. Default:None
WARNING
If you set a DEFAULT_PAGINATION_CLASS, you generally must also configure a PAGE_SIZE, or your pagination class might not know how many items to return per page.
Versioning Settings
DEFAULT_VERSIONING_CLASSThe versioning scheme used for incoming requests. Default:NoneDEFAULT_VERSIONThe fallback version if no versioning information is provided in the request. Default:None
