I’ve done it in one of my project to register v1 and v2 API docs, the trick I did was to wrap the views I want to document in a list of paths that I want to include in the docs, and pass this list of paths to SpectacularAPIView
as urlconf
parameter.
Here is a snippet which should give you an overview:
# api/v1/urls.py
from django.urls import path, include
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView
from rest_framework import routers
v1_router = routers.DefaultRouter()
# ... Register v1 viewsets here
# Define list of v1 routes to include in the OpenAPI schema
schema_urlpatterns = [
path("", include(v1_router.urls)),
]
urlpatterns = schema_urlpatterns + [
path(
"openapi/",
SpectacularAPIView.as_view(
custom_settings={
"TITLE": "API v1",
"SCHEMA_PATH_PREFIX_INSERT": "/v1",
},
urlconf=schema_urlpatterns,
),
name="v1-schema",
),
path(
"docs/",
SpectacularRedocView.as_view(url_name="v1-schema"),
name="v1-docs",
),
]
# api/v2/urls.py
v2_router = routers.DefaultRouter()
# ... Register v2 viewsets here
# Define list of v2 routes to include in the OpenAPI schema
schema_urlpatterns = [
path("", include(v2_router.urls)),
]
urlpatterns = schema_urlpatterns + [
path(
"openapi/",
SpectacularAPIView.as_view(
custom_settings={
"TITLE": "API v2",
"SCHEMA_PATH_PREFIX_INSERT": "/v2",
},
urlconf=schema_urlpatterns,
),
name="v2-schema",
),
path(
"docs/",
SpectacularRedocView.as_view(url_name="v2-schema"),
name="v2-docs",
),
]
# config/urls.py
urlpatterns = [
path("v1/", include("api.v1.urls")),
path("v2/", include("api.v2.urls")),
]
Note that I need to adjust the root of the schema with SCHEMA_PATH_PREFIX_INSERT
, otherwise the URLs are documented without the v1/
or v2/
prefix.
You should be able to adapt this to have a schema for highlevel and one schema for the rest.