Home > Software design >  Get the value of an IntegerChoices object in Django?
Get the value of an IntegerChoices object in Django?

Time:11-23

Suppose I have the following Django (3.2) code:

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

a = AType.ZERO

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

This is very similar to this question, except here we only have the IntegerChoices object, and not an associated model object.

CodePudding user response:

You can use .label:

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