[Django]-Get string from TextChoices class in Django

8👍

Use .label — (Django doc) attribute as

print(Foo.Bar.CODE_A.label)
In [12]: class Bar(models.TextChoices):
    ...:     CODE_A = 'A', "special code A"
    ...:     CODE_B = 'B', "not so special code B"
    ...: 

In [13]: Bar.CODE_A.name
Out[13]: 'CODE_A'

In [14]: Bar.CODE_A.label
Out[14]: 'special code A'
👤JPG

Leave a comment