models.py 674 B

123456789101112131415161718192021222324
  1. """
  2. 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. class Person(models.Model):
  11. GENDER_CHOICES = (
  12. ('M', 'Male'),
  13. ('F', 'Female'),
  14. )
  15. name = models.CharField(max_length=20)
  16. gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
  17. def __str__(self):
  18. return self.name