繁体   English   中英

带有只读字段的 django-rest-swagger 嵌套序列化程序未正确呈现

[英]django-rest-swagger nested serializers with readonly fields not rendered properly

我正在用 django-rest-framework 构建一个 API 并且我开始使用django-rest-swagger进行文档处理。 我有一个带有一些只读字段的嵌套序列化程序,如下所示:

# 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()

在生成的文档中,页面参数部分中的嵌套序列化程序使用字段数据类型呈现,并且没有给出有关其内容的提示,它们就像其他字段一样。

现在您可以看到那里的问题,因为我想通知用户有一个只读字段不应作为嵌套数据的一部分发送,但我看不到这样做的方法。

理想情况是在数据类型列中有 model 描述,就像响应 Class 部分一样。

有什么正确的方法吗?

1.一切请使用drf-yasg文档。

2.你可以在我的一个存储库Kirpi 中找到它的实现并学习如何使用它。

3.如果你在3. ; 有问题,让我知道。

尝试使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM