我正在使用django-rest框架构建一个API,并开始使用django-休息-昂首阔步作为文档。我有一个带有一些read_only字段的嵌套序列化程序,如下所示:
# this is the nested serializer
class Nested(serializers.Serializer):
normal_field = serializers.CharField(help_text="normal")
readonly_field = serializers.CharField(read_only=True,
help_text="readonly")
# this is the parent one
class Parent(serializers.Serializer):
nested_field = Nested()在生成的文档中,页面的参数部分中的嵌套序列化器是用字段数据类型呈现的,没有给出有关其内容的提示,它们与其他字段一样。
现在您可以看到这个问题了,因为我想告诉用户,有一个只读字段不应该作为嵌套数据的一部分发送,但是我看不到这样做的方法。
理想的方法是在Data列中有一个模型描述,就像响应类 部分一样。
是否有适当的方法?
发布于 2019-12-10 21:09:07
发布于 2022-11-25 07:50:13
尝试使用drf_yasg,Swagger会为API生成文档,但这不是绝对正确的!如果您想更正Swagger文档,可以这样做。您需要使用swagger_auto_schema装饰器。下面是示例代码:
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
class ProductSuspendView(CreateAPIView):
@swagger_auto_schema(
tags=['dashboard'],
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'id': openapi.Schema(
type=openapi.TYPE_INTEGER,
description='Id',
),
'suspend_kinds': openapi.Schema(
type=openapi.TYPE_ARRAY,
items=openapi.Items(type=openapi.TYPE_INTEGER),
description='Array suspend (Inappropriate image: 1, Insufficient information: 2, Bad language: 3) (suspend_kinds=[1,2])'
),
}
),
responses={
status.HTTP_200_OK: SuccessResponseSerializer,
status.HTTP_400_BAD_REQUEST: ErrorResponseSerializer
}
)
def post(self, request, *args, **kwargs):
"""
Suspend a product.
"""
...
if success:
return Response({'success': True}, status.HTTP_200_OK)
return Response({'success': False}, status.HTTP_400_BAD_REQUEST)https://stackoverflow.com/questions/29901131
复制相似问题