Settings
Django REST Framework configuration is all namespaced inside a single Django setting named REST_FRAMEWORK. This keeps your settings.py file clean and organized.
General Configuration
Here is an example demonstrating some of the most commonly used global settings:
python
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
If you need to access DRF's settings within your own application code, you should import the api_settings object instead of reading directly from Django's settings.py. This ensures default values are properly applied.
python
from rest_framework.settings import api_settings
print(api_settings.DEFAULT_AUTHENTICATION_CLASSES)Important Settings Categories
- API Policy Settings: Control authentication, permissions, throttling, and filtering behavior.
- Generic View Settings: Control pagination and URL kwarg configurations.
- Content Negotiation Settings: Control renderers, parsers, and metadata classes.
- Format Settings: Control datetime formatting and input validation formats.
- Testing Settings: Control the default formats used by the
APIClient. - Schema Settings: Configure how OpenAPI schemas are automatically generated.
