1

我有一个简单的视图,它将“电子邮件”作为查询参数,我希望将它记录在 OpenAPI 自动生成的模式中。到目前为止,我尝试在 API View 类定义上method_decorator一起应用swagger_auto_schema,但没有成功:

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from django.utils.decorators import method_decorator


@method_decorator(name='retrieve', decorator=swagger_auto_schema(manual_parameters=[
    openapi.Parameter('email', openapi.IN_QUERY, description="Email to be checked", type=openapi.TYPE_STRING)]))
class EmailCheckView(generics.RetrieveAPIView):
    serializer_class = EmailCheckSerializer

    def get_queryset(self):
        email = self.request.query_params.get('email', None)
        if not email:
            raise Http404
        return User.objects.filter(email=self.kwargs['email'])

自动生成的模型仅包含来自序列化程序的主体信息。有什么想法有什么问题吗?

DRF:3.12.2

drf-yasg:1.20.0

我的招摇模式添加到 urls.py 中:

from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
   openapi.Info(
      title="My API",
      default_version='v1',
      description="",
   ),
   public=True,
   permission_classes=[permissions.AllowAny],
)

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

1 回答 1

1

改变

name='retrieve'

name='get'

@method_decorator(
    name="get", # change is here
    decorator=swagger_auto_schema(
        manual_parameters=[
            openapi.Parameter(
                "email",
                openapi.IN_QUERY,
                description="Email to be checked",
                type=openapi.TYPE_STRING,
            )
        ]
    ),
)
class EmailCheckView(generics.RetrieveAPIView):
    serializer_class = EmailCheckSerializer

    def get_queryset(self):
        email = self.request.query_params.get("email", None)
        if not email:
            raise Http404
        return User.objects.filter(email=self.kwargs["email"])

注意:我不确定问题是属于method_decorator(...)还是drf-yasg它本身

于 2021-01-24T15:48:25.880 回答