To remove a specific URL endpoint from drf_yasg, you can use the exclude parameter in the SwaggerUIRenderer class. Here's an example:

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from drf_yasg.renderers import SwaggerUIRenderer

schema_view = get_schema_view(
   openapi.Info(
      title="My API",
      default_version='v1',
      description="My description",
      terms_of_service="https://www.google.com/policies/terms/",
      contact=openapi.Contact(email="contact@mysite.com"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=[permissions.AllowAny],
)

class CustomSwaggerUIRenderer(SwaggerUIRenderer):
    def get_ui_params(self, request):
        ui_params = super().get_ui_params(request)
        ui_params['excluded_urls'] = ['/admin/', '/api-auth/']
        return ui_params

schema_view.renderer_classes = [CustomSwaggerUIRenderer, ]

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

In this example, we create a custom renderer class CustomSwaggerUIRenderer that extends SwaggerUIRenderer. In the get_ui_params method, we call the parent method to get the default UI parameters and then add an excluded_urls parameter that contains a list of URLs that we want to exclude. In this example, we exclude the /admin/ and /api-auth/ URLs.

Finally, we set the renderer_classes of the schema_view to our custom renderer class and add the URL pattern for the Swagger UI.


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

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