将文档添加到 generics.RetrieveAPIView 'retrieve' 方法的查询参数
Add documentation to generics.RetrieveAPIView 'retrieve' method's query param
我有一个简单的视图,它将 'email' 作为查询参数,我想将它记录在 OpenAPI 自动生成的模式中。到目前为止,我尝试在 API 视图 class 定义上应用 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
我的 swagger 架构已添加到 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'),
...
]
改变
name='retrieve'
到
name='get'
@method_decorator(
<b>name="get", # change is here</b>
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
本身
我有一个简单的视图,它将 'email' 作为查询参数,我想将它记录在 OpenAPI 自动生成的模式中。到目前为止,我尝试在 API 视图 class 定义上应用 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
我的 swagger 架构已添加到 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'),
...
]
改变
name='retrieve'
到
name='get'
@method_decorator(
<b>name="get", # change is here</b>
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
本身