$ cat ~/blog/django-tip-get_field_display.md
Django Tip: get_FIELD_display()
Magic template functions in Django
Photo by Aaron Burden on Unsplash
I'm posting this because it seems rather simple, but it took me a while to find - even with some tips from some helpful people in the #django IRC channel.
Let's say you have a ChoiceField set up like the documents describe:
class Foo(models.Model):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
Now, you want to display the gender field in a template. If you use the
variable, you would get "M" or "F" to display on your page. But what if you want "Male" or "Female"? Then you would use.
Nifty.