DRF Spectacular
DRF Spectacular is a powerful and flexible OpenAPI 3 schema generation tool for Django REST Framework. It automates API documentation, offering robust support for Swagger UI and ReDoc. Actively maintained, it receives frequent updates, typically every 1-2 months, incorporating new features, bug fixes, and compatibility with the latest Django and DRF versions.
Warnings
- breaking Pydantic users might see a slightly different schema output due to changes in the serialization method. Review generated schemas carefully after upgrading.
- breaking With the official support for OpenAPI 3.1 and Pydantic>=2, if you use a custom `postprocess_schema_enums` function, you might need to manually add `postprocess_schema_enum_id_removal` to remove temporary IDs.
- gotcha Python 3.6 support has been officially removed due to upstream library breakage. Attempts to use drf-spectacular on Python 3.6 will likely fail.
- gotcha Versions prior to 0.29.0 contained a memory leak during schema generation, particularly noticeable with large or complex APIs.
- gotcha For API endpoints that are serializers marked `many=False` but are incorrectly inferred as lists in the OpenAPI schema, the `forced_singular_serializer` helper function is available.
Install
-
pip install drf-spectacular
Imports
- SpectacularAPIView
from drf_spectacular.views import SpectacularAPIView
- SpectacularSwaggerView
from drf_spectacular.views import SpectacularSwaggerView
- SpectacularRedocView
from drf_spectacular.views import SpectacularRedocView
- extend_schema
from drf_spectacular.utils import extend_schema
- OpenApiParameter
from drf_spectacular.utils import OpenApiParameter
Quickstart
import os
from django.urls import path, include
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView
# --- In your Django settings.py ---
# INSTALLED_APPS = [
# # ...
# 'rest_framework',
# 'drf_spectacular',
# # ...
# ]
#
# SPECTACULAR_SETTINGS = {
# 'TITLE': os.environ.get('DRF_SPECTACULAR_TITLE', 'Your Project API'),
# 'DESCRIPTION': os.environ.get('DRF_SPECTACULAR_DESCRIPTION', 'Your project description'),
# 'VERSION': os.environ.get('DRF_SPECTACULAR_VERSION', '1.0.0'),
# 'SERVE_INCLUDE_SCHEMA': False, # Keep off for production, serve via SpectacularAPIView
# 'SWAGGER_UI_SETTINGS': {
# 'deepLinking': True,
# 'displayRequestDuration': True,
# },
# }
# --- In your Django urls.py ---
urlpatterns = [
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
# Optional UI: path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
# Optional UI: path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
# path('api/', include('your_app.urls')), # Example for your API endpoints
]
# To run a minimal example (requires Django setup)
# from django.conf import settings
# if not settings.configured:
# settings.configure(
# INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes', 'rest_framework', 'drf_spectacular'],
# SPECTACULAR_SETTINGS={'TITLE': 'Test API'},
# ROOT_URLCONF=__name__,
# DEBUG=True
# )
# This is a runnable snippet illustrating the `urls.py` config for drf-spectacular.