Home > OS >  How to display the label from models.TextChoices in the template?
How to display the label from models.TextChoices in the template?

Time:09-16

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? Thanks for your time and help, it is appreciated.

CodePudding user response:

You'd use get_{field_name}_display

Python
modelObj.get_model_choice_display()
Template
{{modelObj.get_model_choice_display}}
  • Related