Django: Display Choice Value
222,498
Solution 1
It looks like you were on the right track - get_FOO_display()
is most certainly what you want:
In templates, you don't include ()
in the name of a method. Do the following:
{{ person.get_gender_display }}
Solution 2
For every field that has choices set, the object will have a get_FOO_display() method, where FOO is the name of the field. This method returns the “human-readable” value of the field.
In Views
person = Person.objects.filter(to_be_listed=True)
context['gender'] = person.get_gender_display()
In Template
{{ person.get_gender_display }}
Documentation of get_FOO_display()
Related videos on Youtube
Author by
Shankze
Updated on February 28, 2022Comments
-
Shankze about 2 years
models.py:
class Person(models.Model): name = models.CharField(max_length=200) CATEGORY_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES) to_be_listed = models.BooleanField(default=True) description = models.CharField(max_length=20000, blank=True)
views.py:
def index(request): latest_person_list2 = Person.objects.filter(to_be_listed=True) return object_list(request, template_name='polls/schol.html', queryset=latest_person_list, paginate_by=5)
On the template, when I call
person.gender
, I get'M'
or'F'
instead of'Male'
or'Female'
.How to display the value (
'Male'
or'Female'
) instead of the code ('M'
/'F'
)?-
Ignacio Vazquez-Abrams over 13 yearspossible duplicate of Django print choices value
-
Shankze over 13 yearsI tried this method, In my template I am using: {% for person in object_list %} some html {{person.get_gender_display() }} html {% endfor %} I am getting the following error: Exception Value: Could not parse the remainder: '()' from 'person.get_gender_display()'
-
Shankze over 13 yearsMy Bad, I should not have included '()' after person.get_gender_display. Its working now. Thanks.
-
Alejandro Garcia about 11 yearsJust a little comment, since gender will only be either M or F(1 character lenght) it has no sense setting the max length of the charfield to
200
. Withmax_lenght=1
is enough, will make your site more efficient and you will ensure that you wont have wrong data. :) -
Ciro Santilli OurBigBook.com almost 8 yearsPossible duplicate of Django templates: verbose version of a choice
-
Underoos almost 5 yearsPossible duplicate of How to get the label of a choice in a Django forms ChoiceField?
-
-
KhoPhi about 9 yearsThis method is priceless. But aside using the method, what's the way to retrieve the value stored in say, {{ i.item.what_to_put_here}} ?
-
jMyles about 9 yearsI don't understand your subquestion. Is what_to_put_here just a method on item? If so, then your syntax is correct. This question about the specific situation of having named choices in a Field object.
-
adam b about 8 yearsJust to note, for anyone using Jinja2 for their templates, you should include the parentheses.
{{ person.get_gender_display() }}
-
DylanYoung about 7 yearsBonus points: what's the best way to do this if you're sending your information via JSON (for instance in a pagination scenario)? Ideally without the overhead of instantiating the Models one by one and calling get_field_display().
-
Marc over 6 yearsGotta love Django, almost 10 years later and the answer is still valid! Thx +1 and drink on me.
-
Anoop Nair about 4 yearsSomehow this doesn't fetch the translated text. Any idea on how to get the translated text if i18 is already implemented on the website?
-
Code-Apprentice about 4 years@AnoopNair If you still need help with figuring out how to display a translated version of the text you should post a new question.
-
benzkji over 3 yearshow to do this if you only specify choices in the admin/modelform (to not trigger migrations when changing them)
-
Roberth Solís almost 3 yearskind of model hack for me hahaha