magic-removal: Added 'choices' model unit test

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1596 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-12-11 22:22:49 +00:00
parent c1be782510
commit 577ac01250
2 changed files with 40 additions and 1 deletions

View File

@ -2,4 +2,4 @@ __all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many',
'ordering', 'lookup', 'get_latest', 'm2m_intermediary', 'one_to_one',
'm2o_recursive', 'm2o_recursive2', 'save_delete_hooks', 'custom_pk',
'subclassing', 'many_to_one_null', 'custom_columns', 'reserved_names',
'or_lookups', 'm2m_multiple']
'or_lookups', 'm2m_multiple', 'choices']

View File

@ -0,0 +1,39 @@
"""
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.core import meta
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
class Person(meta.Model):
name = meta.CharField(maxlength=20)
gender = meta.CharField(maxlength=1, choices=GENDER_CHOICES)
def __repr__(self):
return self.name
API_TESTS = """
>>> a = Person(name='Adrian', gender='M')
>>> a.save()
>>> s = Person(name='Sara', gender='F')
>>> s.save()
>>> a.gender
'M'
>>> s.gender
'F'
>>> a.get_gender_display()
'Male'
>>> s.get_gender_display()
'Female'
"""