/tests/modeltests/choices/models.py

https://code.google.com/p/mango-py/ · Python · 24 lines · 23 code · 0 blank · 1 comment · 0 complexity · 21ff57a6cefceefbfe1467e948d62078 MD5 · raw file

  1. """
  2. 21. Specifying 'choices' for a field
  3. Most fields take a ``choices`` parameter, which should be a tuple of tuples
  4. specifying which are the valid values for that field.
  5. For each field that has ``choices``, a model instance gets a
  6. ``get_fieldname_display()`` method, where ``fieldname`` is the name of the
  7. field. This method returns the "human-readable" value of the field.
  8. """
  9. from django.db import models
  10. GENDER_CHOICES = (
  11. ('M', 'Male'),
  12. ('F', 'Female'),
  13. )
  14. class Person(models.Model):
  15. name = models.CharField(max_length=20)
  16. gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
  17. def __unicode__(self):
  18. return self.name