Skip to main content
Schema generation allows automatic documentation of your API using OpenAPI or CoreAPI formats.

get_schema_view

Return a schema view for your API.
from rest_framework.schemas import get_schema_view

schema_view = get_schema_view(
    title='My API',
    description='API documentation',
    version='1.0.0',
)

Parameters

title
str
The title of your API
url
str
The base URL for your API
description
str
A description of your API
urlconf
str
Django URL configuration module to use. Default is None (use default)
renderer_classes
list
List of renderer classes to use for the schema view
public
bool
If True, include all endpoints. If False, only include endpoints the user has permission to access. Default is False
patterns
list
List of URL patterns to include in the schema. Default is None (use all)
generator_class
class
Schema generator class to use. Default is None (auto-detect)
authentication_classes
list
Authentication classes for the schema view
permission_classes
list
Permission classes for the schema view
version
str
API version string

SchemaGenerator

Generates OpenAPI schemas from URL patterns.
from rest_framework.schemas.openapi import SchemaGenerator

generator = SchemaGenerator(
    title='My API',
    version='1.0.0',
    description='API documentation',
)
schema = generator.get_schema()

Methods

get_schema(request=None, public=False)

Generate and return the OpenAPI schema.
request
Request
Request instance for permission checking
public
bool
If True, include all endpoints regardless of permissions

get_info()

Return the info section of the OpenAPI schema.

AutoSchema

Per-endpoint view introspection for automatic schema generation.
from rest_framework.schemas.openapi import AutoSchema
from rest_framework.views import APIView

class MyView(APIView):
    schema = AutoSchema(
        tags=['my-tag'],
        operation_id_base='MyOperation',
        component_name='MyComponent',
    )

Constructor

tags
list
List of tags for the operation
operation_id_base
str
Base name for operation IDs. If empty, deduced from Model/Serializer/View name
component_name
str
Component name for the schema. If empty, deduced from Serializer class name

Methods

get_operation(path, method)

Generate the OpenAPI operation object for this endpoint.
path
str
required
URL path for the endpoint
method
str
required
HTTP method

get_components(path, method)

Return component schemas from the serializer.

get_operation_id(path, method)

Compute the operation ID for this endpoint.

get_description(path, method)

Get the description for the operation from the view’s docstring.

get_path_parameters(path, method)

Return path parameters from templated URL variables.

get_filter_parameters(path, method)

Return query parameters from filter backends.

get_pagination_parameters(path, method)

Return query parameters from pagination class.

get_request_body(path, method)

Return the OpenAPI request body schema.

get_responses(path, method)

Return the OpenAPI responses schema.

ManualSchema

Manually define the schema for a view.
from rest_framework.schemas import ManualSchema
import coreapi

schema = ManualSchema(
    fields=[
        coreapi.Field(
            name='search',
            required=False,
            location='query',
            description='Search term'
        ),
    ],
    description='List of items matching search criteria'
)

class MyView(APIView):
    schema = schema

Constructor

fields
list
required
List of coreapi.Field objects defining the schema
encoding
str
The encoding for the request body
description
str
Description for the endpoint

DefaultSchema

Simple schema with no introspection.
from rest_framework.schemas.inspectors import DefaultSchema

class MyView(APIView):
    schema = DefaultSchema()

Example: Complete Setup

URLs Configuration

from django.urls import path
from rest_framework.schemas import get_schema_view

schema_view = get_schema_view(
    title='My API',
    description='Complete API documentation',
    version='1.0.0',
    public=True,
)

urlpatterns = [
    path('openapi/', schema_view, name='openapi-schema'),
    # ... your API URLs
]

Custom AutoSchema

from rest_framework.schemas.openapi import AutoSchema

class CustomAutoSchema(AutoSchema):
    def get_operation(self, path, method):
        operation = super().get_operation(path, method)
        # Add custom fields
        operation['x-custom-field'] = 'custom-value'
        return operation

class MyView(APIView):
    schema = CustomAutoSchema(tags=['custom'])

Disable Schema for Specific View

from rest_framework.views import APIView

class MyView(APIView):
    schema = None  # Exclude from schema generation

Build docs developers (and LLMs) love