How do I display the human readable name of a choice?

17,002 views
Skip to first unread message

Kevin Audleman

unread,
Feb 10, 2009, 9:23:15 PM2/10/09
to Django users
Hi everyone,

I've set up a model that has a state field with the input set to a
list of states,

state = models.CharField(max_length=100, blank=True,
choices=US_STATE_LIST)

The drop-down on the edit form works great, displaying 'Alabama' and
storing AL in the database. However when I view it, I see AL. How do I
get django to display the human readable name?

I would prefer to do this on the template level, as I'm using a view
from a contributed app that I can't modify.

Thanks,
Kevin

Alex Gaynor

unread,
Feb 10, 2009, 9:24:56 PM2/10/09
to django...@googlegroups.com
http://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#get-foo-display

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Kevin Audleman

unread,
Feb 10, 2009, 11:39:42 PM2/10/09
to Django users
Thanks Alex, however this is a solution at the View level, and I'm
using a view that I didn't write. Is there also a way to do this at
the template level?

Thanks again,
Kevin

On Feb 10, 1:24 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Tue, Feb 10, 2009 at 4:23 PM, Kevin Audleman <kevin.audle...@gmail.com>wrote:
>
>
>
>
>
> > Hi everyone,
>
> > I've set up a model that has a state field with the input set to a
> > list of states,
>
> > state = models.CharField(max_length=100, blank=True,
> > choices=US_STATE_LIST)
>
> > The drop-down on the edit form works great, displaying 'Alabama'  and
> > storing AL in the database. However when I view it, I see AL. How do I
> > get django to display the human readable name?
>
> > I would prefer to do this on the template level, as I'm using a view
> > from a contributed app that I can't modify.
>
> > Thanks,
> > Kevin
>
> http://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddo...

Alex Gaynor

unread,
Feb 10, 2009, 11:41:47 PM2/10/09
to django...@googlegroups.com
Yes, the same thing works {{ model.get_FIELD_display }}.

Kevin Audleman

unread,
Feb 11, 2009, 5:13:01 PM2/11/09
to Django users
I could've swore I tried that before posting my question and it didn't
work, but I tried it this time and it did. Anyhow, a long winded way
of apologizing for asking a simple question. Thank you for taking the
time to answer!

Kevin

On Feb 10, 3:41 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:

Aniket Zamwar

unread,
Nov 26, 2013, 8:06:28 AM11/26/13
to django...@googlegroups.com, kevin.a...@gmail.com
This function gets for a specific information stored in the instance of the model.
If I want to display as a drop down in the template all the human readable names in the choices can I do that ?

Also, Before storing it to DB, how to convert it back to human redable to short version mentioned in CHOICES used to store in DB ?

Vibhu Rishi

unread,
Nov 28, 2013, 3:48:05 PM11/28/13
to django...@googlegroups.com, kevin.a...@gmail.com
you just need to enclose it in a get_<attribute name>_display and it will display it in the template file.

in your example below, you will need to show it as follows in your templates html file:
object.get_state_display

where object is whatever the object name you have passed via the view.

Hope this helps.

V.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/13ff53a8-b905-401c-80ae-87da07f79c90%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Simplicity is the ultimate sophistication. - Leonardo da Vinci
Life is really simple, but we insist on making it complicated. - Confucius

Ousseynou Diop

unread,
Sep 6, 2018, 4:08:38 PM9/6/18
to Django users
Hi friend ,

use this function to display the choices field.

models.py


class Article(models.Model):
   
    ARTICLE_CHOICES = (

)    


    status = models.CharField(max_length=120, choices=ARTICLE_CHOICES, )

in your templates

{% article in articles %}

<p>{{artcile.get_status_display}}</p>


{% endfor %}

Joel

unread,
Sep 7, 2018, 1:30:56 AM9/7/18
to django...@googlegroups.com
This is clearly described in the polls application tutorial. https://docs.djangoproject.com/en/2.1/intro/tutorial01/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages