How to display the label from models.TextChoices in the template?
Question:
The Django docs says that one can use .label
, but it does not work in the template.
class Model(models.Model):
class ModelChoices(models.TextChoices):
ENUM = 'VALUE', 'Label'
model_choice = models.CharField(choices=ModelChoices.choices)
In the template object.model_choice
displays the value ('VALUE'
).
object.model_choice.label
displays nothing.
How is it possible to get the label ('Label'
) in the template?
Answers:
You’d use get_{field_name}_display
Python
modelObj.get_model_choice_display()
Template
{{modelObj.get_model_choice_display}}
The Django docs says that one can use .label
, but it does not work in the template.
class Model(models.Model):
class ModelChoices(models.TextChoices):
ENUM = 'VALUE', 'Label'
model_choice = models.CharField(choices=ModelChoices.choices)
In the template object.model_choice
displays the value ('VALUE'
).
object.model_choice.label
displays nothing.
How is it possible to get the label ('Label'
) in the template?
You’d use get_{field_name}_display
Python
modelObj.get_model_choice_display()
Template
{{modelObj.get_model_choice_display}}