get_FOO_display

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
get_FOO_display()
GENDER_CHOICES = (
('1', 'Male'),
('0', 'Female'),
)
class Person(models.Model):
name = models.CharField(max_length=20)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
>>> p = Person(name='John', gender='1')
>>> p.save()
>>> p.gender
'1'
>>> p.get_gender_display()
'Male'
###################################################################
# view
@render_to('test.html')
def test(request,id,name):
person = Person.objects.all()
return {'person':person}
###################################################################
# template
{% for i in person %}
{{ i.get_gender_display }}
{% endfor %}