Is it possible to return the display name of a choice in Django?
So when you make choices for a form/model you do this:
EXP_CHOICES = (
('1ST','First'),
('2ND','Second'),
.......
('XXX','YYY'),
) # where XXX is the value stored in the database and YYY is what you see in a form.
This also applies to the queries, so when you have the mentioned choices and select one, do a query on the model and it returns '1ST'/'2ND'/'XXX'.
But what if you want to display the YYY instead? So we know that '1ST' is 'First', but we want to make it user readable.
Is there a way to access that EXP_CHOICES and write out the YYY tied to them, or you must make an if&else/selector to be like
{% if data.chosen == '1ST'%}
First
{% elif ... %}
...
{% endif %}
You use .get_fieldname_display(…)
[Django-doc], so if the model field is chosen
, then it is get_chosen_display
:
{{ data.get_chosen_display }}