Oct 03

Django Tip: get_FIELD_display()

Tag: YadaDustin @ 3:45 pm

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 {{ person.gender }} variable, you would get “M” or “F” to display on your page. But what if you want “Male” or “Female”? Then you would use {{ person.get_gender_display }}.

Nifty.

3 Responses to “Django Tip: get_FIELD_display()”

Mike Riley from says:

I’m just starting out in django, it’s funny how a lot of it (like these smart functions) is so similar to Ruby on Rails.

Kevin from Great Wall of China says:

WOW. This is a small code. I thought it would be a lot longer.

Aaron from Birth of Mothra says:

I was also noticing that it reminds me of ROR… Good stuff.

Leave a Reply

This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.



ss_blog_claim=4973c51f5e7bec57951f995fed1b85f3ss_blog_claim=4973c51f5e7bec57951f995fed1b85f3


You are viewing a mobilized version of this site...
View original page here

How do you rate mobile version of this page?

Mobilized by Mowser Mowser