在 Django 中获取 IntegerChoices 对象的值?

Get the value of an IntegerChoices object in Django?

假设我有以下 Django (3.2) 代码:

class AType(models.IntegerChoices):
    ZERO = 0, 'Zero'
    ONE = 1, 'One'
    TWO = 2, 'Two'

a = AType.ZERO

如何获得“零”(与 a 关联的字符串)?

这与 非常相似,只是这里我们只有 IntegerChoices 对象,而不是关联的模型对象。

您可以使用 .label:

>>> a = AType.ZERO
>>> a.label
'Zero'