INSTALLED_APPS += ['drf_spectacular']
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}
SPECTACULAR_SETTINGS = {
'TITLE': 'My API',
'DESCRIPTION': 'API for my awesome project',
'VERSION': '1.0.0',
'SERVE_INCLUDE_SCHEMA': False,
}
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from django.urls import path
urlpatterns = [
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
]
from drf_spectacular.utils import extend_schema, OpenApiParameter
from rest_framework import viewsets
class PostViewSet(viewsets.ModelViewSet):
@extend_schema(
summary='List all posts',
description='Returns a paginated list of published posts',
parameters=[
OpenApiParameter(
name='category',
description='Filter by category slug',
required=False,
type=str
),
]
)
def list(self, request):
"""List posts with optional category filter."""
return super().list(request)
DRF auto-generates API schemas and documentation. I use drf-spectacular for OpenAPI 3.0 schemas. The schema describes endpoints, parameters, and responses. I customize with decorators like @extend_schema. Interactive docs via Swagger UI or ReDoc let developers explore the API. I document parameters with type hints and docstrings. Schema generation works with serializers and viewsets automatically. For public APIs, good documentation is essential. I version schemas alongside API versions.