有没有一种方法可以更改DRF-Spectacular中序列化器的自动化shema名称
我得到了许多名为“ InputSerializer”和“ OutputSerializer”的序列化器,它们在DRF-Spectacular中转化为“输入”和“输出”模式名称。这最终将API端点引用到同一模式。有没有一种方法可以覆盖这些连续剧器的自动化架构名称而不更改班级名称?
I got many serializers named 'InputSerializer' and 'OutputSerializer' which translates to 'Input' and 'Output' schema name in drf-spectacular. This ends up referring the api endpoints to the same schema. Is there a way to override the autogenerated schema names of these serializers without changing the name of the class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了一堆,但从未尝试过解决它。查看我找到的文档 Extended_schema_serializer ,可能会做您需要的事情。这是
component_name
- 覆盖默认的类名称提取其较长而丑陋,但可以由装饰器上的装饰器修复:d
eding :
我最终实现了这一点。这是我写的小包装纸。这只是使序列化和字段之间的较短和一致的事情。
您可以在序列化器上多次使用
extended_schema_serializer
,因此不会破坏任何内容。I've run into this a bunch, but never tried to solve it. Looking at the docs I found
extended_schema_serializer
, which might do what you need. Here is the full api, and the relevant point:component_name
– override default class name extractionIts kinda long and ugly, but that can be fixed by a decorator on the decorator :D
Edit:
I ended up implementing this. Here is the small wrapper I wrote. It just makes things shorter and consistent between serializers and fields.
You can use
extended_schema_serializer
more than once on a serializer, so this won't break anything.可以理解的,但是一个独特的名称必须来自某个地方,而壮观的名字不知道什么是classname本身对您来说是什么好名字。
安德鲁(Andrew)提供了天然溶液,但也有句法糖(与DRF-YASG的兼容性特征)。
否则,我建议子分类
autoschema
和覆盖 _get_serializer_name _get_serialize_name ,并使其适合您。Understandable, but a unique name has to come from somewhere and spectacular cannot know what is a good name for you other than the classname itself.
Andrew provides the native solution but there is also syntactic sugar for that (compatibility feature with drf-yasg).
https://drf-spectacular.readthedocs.io/en/latest/drf_yasg.html?highlight=ref_name#compatibility
Otherwise I would recommend subclassing
AutoSchema
and overriding_get_serializer_name
and make it work for you.