1

Suppose I have the following Django (3.2) code:

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

class A(models.Model):
    a_type = models.IntegerField(choices=AType.choices)

Also, that I have a Django template with a variable a:

{{a.a_type}} is a number.

How do I get "Zero" (the string associated with a_type)?

dfrankow
  • 20,191
  • 41
  • 152
  • 214

1 Answers1

3

You can use the get_FOO_display() instance method:

{{ a.get_a_type_display }} is a number
dfrankow
  • 20,191
  • 41
  • 152
  • 214
Xavier Brassoud
  • 697
  • 6
  • 14