如何在 Django 中全局更改 UUID 字段表示?

问题描述 投票:0回答:0

我在 Django DRF 项目中用

UUID
id 定义了各种模型:

id = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)

但是,

to_representation
的默认
rest_framework.serializers.UUIDField
实现是:

valid_formats = ('hex_verbose', 'hex', 'int', 'urn')

def to_representation(self, value):
    if self.uuid_format == 'hex_verbose':
        return str(value)
    else:
        return getattr(value, self.uuid_format)

因此项目中所有API路由的输出将UUID输出呈现为字符串,除以破折号:

"id": "86582c7d-a10e-4e2e-be42-8e045ca0fa00"
,我想做的是将所有项目序列化器/路由中UUID的全局表示更改为十六进制(删除破折号),
"id": "86582c7da10e4e2ebe428e045ca0fa00"
.

有谁知道我如何解决休息框架以更改所有 UUID 的输出?

我不想更改任何预定义模型、序列化程序或视图!

python django django-rest-framework uuid
© www.soinside.com 2019 - 2024. All rights reserved.