2006-05-02 03:31:56 +02: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 12:32:08 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2011-10-13 20:04:12 +02:00
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
GENDER_CHOICES = (
|
|
|
|
('M', 'Male'),
|
|
|
|
('F', 'Female'),
|
|
|
|
)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 03:31:56 +02:00
|
|
|
class Person(models.Model):
|
2007-08-05 07:14:46 +02:00
|
|
|
name = models.CharField(max_length=20)
|
|
|
|
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2006-05-02 03:31:56 +02:00
|
|
|
return self.name
|