2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
21. Specifying 'choices' for a field
|
|
|
|
|
|
|
|
Most fields take a ``choices`` parameter, which should be a tuple of tuples
|
|
|
|
specifying which are the valid values for that field.
|
|
|
|
|
|
|
|
For each field that has ``choices``, a model instance gets a
|
|
|
|
``get_fieldname_display()`` method, where ``fieldname`` is the name of the
|
|
|
|
field. This method returns the "human-readable" value of the field.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
GENDER_CHOICES = (
|
|
|
|
('M', 'Male'),
|
|
|
|
('F', 'Female'),
|
|
|
|
)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 09:31:56 +08:00
|
|
|
class Person(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=20)
|
|
|
|
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|