📝 Django

Swagger Documentation with drf-spectacular

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
106
🌳
Level
Advanced

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

Installation

pip install drf-spectacular
INSTALLED_APPS = [
    ...
    'drf_spectacular',
]

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

Schema Configuration

SPECTACULAR_SETTINGS = {
    'TITLE': 'Task Manager API',
    'DESCRIPTION': 'API for task management',
    'VERSION': '1.0.0',
    'SERVE_INCLUDE_SCHEMA': False,
}

URLs

from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView

urlpatterns = [
    path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
    path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    path('api/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]

Open /api/docs/ for the interactive documentation.

Annotations

from drf_spectacular.utils import extend_schema, OpenApiParameter

class TaskViewSet(viewsets.ModelViewSet):

    @extend_schema(
        summary='List tasks',
        description='Returns tasks belonging to the current user',
        parameters=[
            OpenApiParameter('status', str, description='Filter by status'),
        ],
    )
    def list(self, request):
        return super().list(request)

    @extend_schema(summary='Create a task')
    def create(self, request):
        return super().create(request)

Documenting a Serializer

class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = ['id', 'title', 'status']
        # drf-spectacular automatically generates descriptions from the model

Exporting the Schema to a File

python manage.py spectacular --color --file schema.yml

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 132
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 84
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 73

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!