2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Specifying 'choices' for a field
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Person(models.Model):
|
2018-12-10 22:28:49 +08:00
|
|
|
GENDER_CHOICES = (
|
|
|
|
('M', 'Male'),
|
|
|
|
('F', 'Female'),
|
|
|
|
)
|
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
|