Display Tuple Value for Choices Field in Django

Posted by: scoopseven 13 years ago

I know, this is an odd name for a post. I'm trying to explain this:
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.
When you define a field on a model and set it to a CHOICES field, like this: gender = models.CharField(max_length=1, choices=GENDER_CHOICES) where GENDER_CHOICES is a tuple, the data that's stored in that field is a reference (often an integer) that doesn't represent the actual value of the field. So when you need to display the actual value, rather than the key that's stored in the field, you need to do this. Model.get_FOO_display() where FOO is your field name. Here's the official documentation.

Currently unrated


Recent Tweets

Recent Posts

Archive

2013
2012
2011
2010
2009
2008
2007
2006

Categories

Authors

Feeds

RSS / Atom