/tests/modeltests/choices/models.py
Python | 24 lines | 23 code | 0 blank | 1 comment | 0 complexity | 21ff57a6cefceefbfe1467e948d62078 MD5 | raw file
Possible License(s): BSD-3-Clause
1""" 221. Specifying 'choices' for a field 3 4Most fields take a ``choices`` parameter, which should be a tuple of tuples 5specifying which are the valid values for that field. 6 7For each field that has ``choices``, a model instance gets a 8``get_fieldname_display()`` method, where ``fieldname`` is the name of the 9field. This method returns the "human-readable" value of the field. 10""" 11 12from django.db import models 13 14GENDER_CHOICES = ( 15 ('M', 'Male'), 16 ('F', 'Female'), 17) 18 19class Person(models.Model): 20 name = models.CharField(max_length=20) 21 gender = models.CharField(max_length=1, choices=GENDER_CHOICES) 22 23 def __unicode__(self): 24 return self.name