[英]django-rest-swagger nested serializers with readonly fields not rendered properly
I'm building an API with django-rest-framework and I started using django-rest-swagger for documentation.我正在用 django-rest-framework 构建一个 API 并且我开始使用django-rest-swagger进行文档处理。 I have a nested serializer with some read_only fields, like this:
我有一个带有一些只读字段的嵌套序列化程序,如下所示:
# 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()
In the generated docs, nested serializers in the Parameters part of the page are rendered with field data type and no hint is given about its content, they are just like other fields.在生成的文档中,页面参数部分中的嵌套序列化程序使用字段数据类型呈现,并且没有给出有关其内容的提示,它们就像其他字段一样。
Now you can see the problem there, as I would like to inform the user that there is a readonly field that should not be sent as part of the nested data but I can not see a way of doing so.现在您可以看到那里的问题,因为我想通知用户有一个只读字段不应作为嵌套数据的一部分发送,但我看不到这样做的方法。
The ideal would be having a model description in Data Type column, just like the Response Class section .理想情况是在数据类型列中有 model 描述,就像响应 Class 部分一样。
Is there any proper way of doing so?有什么正确的方法吗?
Try to use drf_yasg
instead, Swagger will generate the documentation for APIs, but it's not absolutely right, If you want to correct Swagger documentation.尝试使用
drf_yasg
代替,Swagger 将生成 API 的文档,但它不是绝对正确的,如果你想更正 Swagger 文档。 you can do it.你能行的。 You will need to use
swagger_auto_schema
decorator.您将需要使用
swagger_auto_schema
装饰器。 Below is the sample code:下面是示例代码:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.