models.py 750 Bytes
Newer Older
1
"""
2
Specifying 'choices' for a field
3 4 5 6 7 8 9 10 11 12

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
13
from django.utils.encoding import python_2_unicode_compatible
14 15 16 17 18 19

GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
)

Jason Myers's avatar
Jason Myers committed
20

21
@python_2_unicode_compatible
22
class Person(models.Model):
23 24
    name = models.CharField(max_length=20)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
25

26
    def __str__(self):
27
        return self.name