Skip to content

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.

python
# 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.

python
# 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_CLASSES Determines the default set of renderers for response formatting. Default: ['rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer']

  • DEFAULT_PARSER_CLASSES Determines how incoming request.data is parsed. Default: ['rest_framework.parsers.JSONParser', 'rest_framework.parsers.FormParser', 'rest_framework.parsers.MultiPartParser']

  • DEFAULT_AUTHENTICATION_CLASSES Defines the authenticators used to populate request.user. Default: ['rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication']

  • DEFAULT_PERMISSION_CLASSES Sets the default permission checks for views. Default: ['rest_framework.permissions.AllowAny']

  • DEFAULT_THROTTLE_CLASSES Sets the default throttling policies. Default: [] (Empty list)

Generic View Settings

These settings control the behavior of generic class-based views.

  • DEFAULT_PAGINATION_CLASS The pagination style for querysets. Default: None (Pagination is disabled)

  • PAGE_SIZE The 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_CLASS The versioning scheme used for incoming requests. Default: None

  • DEFAULT_VERSION The fallback version if no versioning information is provided in the request. Default: None