简体   繁体   English

如何在 drf-yasg swagger_auto_schema request_body 上指定示例值?

[英]how to specify example value on drf-yasg swagger_auto_schema request_body?

I everyone.我大家。 My question is similar to the one planted here , but in this case I would like to do the same with the request_body parameter.我的问题与种植在这里的问题相似,但在这种情况下,我想对 request_body 参数做同样的事情。 How can I add an example value for the request_body parameter, similary as it is done there with the response parameter?如何为 request_body 参数添加示例值,类似于使用 response 参数在此处完成的操作? Thanks for your attention and your help感谢您的关注和帮助

You should use the example parameter.您应该使用示例参数。 Look at this example:看这个例子:

request_schema_dict = openapi.Schema(
    title=_("Update order"),
    type=openapi.TYPE_OBJECT,
    properties={
        'ordered_items': openapi.Schema(type=openapi.TYPE_ARRAY, description=_('Ordered items list'), 
            items=openapi.Schema(type=openapi.TYPE_OBJECT, description=_('Ordered item'),
                properties={
                    'item': openapi.Schema(type=openapi.TYPE_STRING, description=_('Item id'), example="123*123*0001"),
                    'quantity': openapi.Schema(type=openapi.TYPE_NUMBER, description=_('Ordered quantity'), example=12.33),
                    'sequence_number': openapi.Schema(type=openapi.TYPE_INTEGER, 
                    description=_('Sequence of item inclusion in the order.'), example=1),
                    }
            )
        ),
        'status': openapi.Schema(type=openapi.TYPE_STRING, description=_('Order status'), example=1, enum=[0,1,2,3,4,5]),
        'invoicing_date': openapi.Schema(type=openapi.TYPE_STRING, description=_('Invoice date'), 
        example="2022-05-27T12:48:07.256Z", format="YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]"),
        'invoice_number': openapi.Schema(type=openapi.TYPE_STRING, description=_('Invoice number'), example="123456789"),
        'note': openapi.Schema(type=openapi.TYPE_STRING, description=_('Client user note'), example=_("Client user note")),
        'agent_note': openapi.Schema(type=openapi.TYPE_STRING, description=_('Agent note'), example=_("Agent note")),
    }
)

@swagger_auto_schema(request_body=request_schema_dict, responses={200: 'Order updated.'}) 
def put(self, request, id):
..............

This will generate the following output on swagger:这将在 swagger 上生成以下输出: 在此处输入图像描述

暂无
暂无

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

相关问题 Django - drf-yasg 将 @swagger_auto_schema 中的 request_body 设置为 @api_view 上的序列化程序的简化版本 - Django - drf-yasg setting request_body in @swagger_auto_schema to reduced version of serializer on @api_view drf_yasg @swagger_auto_schema 未显示 POST 请求所需的参数 - drf_yasg @swagger_auto_schema not showing the required parameters for POST Request 如何使用 drf-yasg 自动生成的招摇页面配置“HTTPS”方案? - How can I configure "HTTPS" schemes with the drf-yasg auto-generated swagger page? Django 2.x drf-yasg如何在自定义方法中创建API(如在swagger中) - Django 2.x drf-yasg how to create API in a custom method (like in swagger) 如何使用 swagger_auto_schema 将方法标记为已弃用? - How to mark a method as deprecated with swagger_auto_schema? 在 Django swagger_auto_schema' 中未定义 - In Django swagger_auto_schema' is not defined Django 如何在作为所有其他视图父级的类基视图中使用 drf-yasg 生成 swagger 文档? - Django How to use drf-yasg in class-base view which is parent of all other views to generate swagger docs? Django, drf-yasg - 如何为标签添加描述? - Django, drf-yasg - how to add description to tags? 使用 drf-yasg,我如何显示多个 openapi 方案? - With drf-yasg, how can i show multiple openapi schemes? Django rest 框架 drf-yasg swagger ListField 序列化程序的多个文件上传错误 - Django rest framework drf-yasg swagger multiple file upload error for ListField serializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM