在Django中获得一个IntegerChoices字段的值?



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

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

另外,我有一个Django模板变量a:

{{a.a_type}} is a number.

如何得到"0"(与a_type相关联的字符串)?

可以使用get_FOO_display()实例方法:

{{ a.get_a_type_display }} is a number

最新更新