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?

Asked By: Hills

||

Answers:

You’d use get_{field_name}_display

Python
modelObj.get_model_choice_display()
Template
{{modelObj.get_model_choice_display}}
Answered By: Nealium
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.