有没有一种方法可以更改DRF-Spectacular中序列化器的自动化shema名称

发布于 2025-01-31 19:21:13 字数 146 浏览 3 评论 0原文

我得到了许多名为“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽手叙旧 2025-02-07 19:21:14

我遇到了一堆,但从未尝试过解决它。查看我找到的文档 Extended_schema_serializer ,可能会做您需要的事情。这是

  • component_name - 覆盖默认的类名称提取
@extended_schema_serializer(component_name="SomeNiceReallyLongId")
class Input(Serializer):
    # pass

其较长而丑陋,但可以由装饰器上的装饰器修复:d

eding

我最终实现了这一点。这是我写的小包装纸。这只是使序列化和字段之间的较短和一致的事情。

您可以在序列化器上多次使用extended_schema_serializer,因此不会破坏任何内容。

@oapi.name("UserEditRequest")
@extend_schema_serializer(examples=[]) # other settings
class EditSerializer(ModelSerializer):
    pass
# oapi.py
from drf_spectacular.utils import set_override as _set_override

def name(val: str):
    def decorator(klass):
        if issubclass(klass, BaseSerializer):
            _set_override(klass, "component_name", val)
        elif isinstance(klass, Field):
            _set_override(klass, "field_component_name", val)
        else:
            raise Exception(f"Unhandled class: {klass}")

        return klass

    return decorator

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 extraction
@extended_schema_serializer(component_name="SomeNiceReallyLongId")
class Input(Serializer):
    # pass

Its 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.

@oapi.name("UserEditRequest")
@extend_schema_serializer(examples=[]) # other settings
class EditSerializer(ModelSerializer):
    pass
# oapi.py
from drf_spectacular.utils import set_override as _set_override

def name(val: str):
    def decorator(klass):
        if issubclass(klass, BaseSerializer):
            _set_override(klass, "component_name", val)
        elif isinstance(klass, Field):
            _set_override(klass, "field_component_name", val)
        else:
            raise Exception(f"Unhandled class: {klass}")

        return klass

    return decorator
属性 2025-02-07 19:21:14

可以理解的,但是一个独特的名称必须来自某个地方,而壮观的名字不知道什么是classname本身对您来说是什么好名字。
安德鲁(Andrew)提供了天然溶液,但也有句法糖(与DRF-YASG的兼容性特征)。

class InputSerializer(serializer.Serializer):

    class Meta:
        ref_name = 'SomeNiceReallyLongId'

否则,我建议子分类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).

class InputSerializer(serializer.Serializer):

    class Meta:
        ref_name = 'SomeNiceReallyLongId'

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文