To exclude function-based API views from drf_yasg, you can use the exclude parameter in the AutoSchema class. Here's an example:

from rest_framework.decorators import api_view
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from drf_yasg.inspectors import SwaggerAutoSchema

# Function-based view
@api_view()
def my_view(request):
    """
    My function-based view.
    """
    ...

# Exclude function-based views from schema
class ExcludeFunctionBasedSchema(SwaggerAutoSchema):
    def should_filter(self, path, method, view):
        return not hasattr(view, 'get_serializer_class')


schema_view = get_schema_view(
    openapi.Info(
        title="My API",
        default_version="v1",
        description="My API description",
        terms_of_service="https://example.com/terms/",
        contact=openapi.Contact(email="contact@example.com"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=[permissions.AllowAny],
    # Use excluded schema
    schema_generator_class=ExcludeFunctionBasedSchema,
)

urlpatterns = [
    # Your URLs here...
    ...
]

In this example, we define a function-based view my_view and a custom SwaggerAutoSchema class ExcludeFunctionBasedSchema that excludes function-based views from the schema. We then use the schema_generator_class parameter in the get_schema_view function to use the excluded schema.

Note: This example uses hasattr(view, 'get_serializer_class') as a condition to exclude function-based views. You can modify this condition to suit your needs.


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

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