To remove the API endpoint from the DRF_YASG Swagger documentation, you can exclude it by specifying it in the exclude_patterns list in your urls.py file.

For example, if your API endpoint is located at /api/, you can exclude it by adding the following code to your urls.py file:

from django.urls import path, include
from rest_framework import routers
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

router = routers.DefaultRouter()

# Register your API views here using router.register()

schema_view = get_schema_view(
    openapi.Info(
        title="Your API Documentation",
        default_version='v1',
        description="API documentation for your project",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="contact@yourproject.local"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=[permissions.AllowAny],
    patterns=[],
    urlconf='yourproject.urls',
)

exclude_patterns = ['/api/']  # Add your excluded API endpoints here

urlpatterns = [
    path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    path('api/', include(router.urls)),
]

# Add excluded patterns to schema view
schema_view.patterns = [path(p, schema_view) for p in exclude_patterns]

This will exclude the /api/ endpoint from the Swagger documentation. You can add more excluded patterns to the exclude_patterns list as needed.


原文地址: https://www.cveoy.top/t/topic/bsYi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录