gpt4 book ai didi

python - Django,drf-yasg - 如何向标签添加描述?

转载 作者:行者123 更新时间:2023-12-03 13:34:09 29 4
gpt4 key购买 nike

Swagger 文档说你可以这样做:
https://swagger.io/docs/specification/grouping-operations-with-tags/
但不幸的是 drf-yasg 没有实现这个功能:
https://github.com/axnsan12/drf-yasg/issues/454
据说,我可以添加自定义生成器类,但这是一个非常笼统的答案。现在我看到 drf_yasg.openapi.Swagger获取 info block ,我有想法,这可能是放置 global tags 的正确位置部分作为额外的 init 参数,但它比自定义生成器类更深,我对此模块缺乏了解
有没有人有这个特定问题的解决方案,或者至少可能是某种教程的链接,如何正确自定义生成器类?

最佳答案

不确定这是否正是您正在寻找的,但我认为它可能会有所帮助。
要设置标签,我使用 @swagger_auto_schema decorator , 可以通过几种不同的方式应用,主要取决于 Views 的类型用于您的项目。完整的详细信息可以在文档 here 上找到.
使用 Views 时源自 APIView ,你可以这样做:

class ClientView(APIView):
@swagger_auto_schema(tags=['my custom tag'])
def get(self, request, client_id=None):
pass
根据 docs , 限制是 tags仅将 strs 列表作为值。因此,从这里开始,我相信不再支持标签上的额外属性,如 Swagger 文档 here 中所述。 .
无论如何,如果您只需要定义摘要或描述来获得如下图所示的内容,则可以使用装饰器或类级别的文档字符串来定义它们。这是一个例子:
tag with desc and summary
enter image description here
class ClientView(APIView):
'''
get:
Client List serialized as JSON.

This is a description from a class level docstring.

'''
def get(self, request, client_id=None):
pass

@swagger_auto_schema(
operation_description="POST description override using
decorator",
operation_summary="this is the summary from decorator",

# request_body is used to specify parameters
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
required=['name'],
properties={
'name': openapi.Schema(type=openapi.TYPE_STRING),
},
),
tags=['my custom tag']
)
def post(self, request):
pass

祝你好运!

关于python - Django,drf-yasg - 如何向标签添加描述?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62572389/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com