Ticket #10531: 10531-patch.diff

File 10531-patch.diff, 1.2 KB (added by kc9ddi, 15 years ago)
  • docs/topics/db/models.txt

     
    171171                ('SR', 'Senior'),
    172172                ('GR', 'Graduate'),
    173173            )
     174       
     175        The first element in each tuple is the value that will be stored in the database, the second
     176        element will be displayed by the admin interface, or in a ModelChoiceField.  Given an instance
     177        of a model object, the display value for a choices field can be accessed using the ``get_FOO_display``
     178        method.  For example::
     179       
     180                from django.db import models
     181               
     182                class Person(models.Model):
     183                        GENDER_CHOICES = (
     184                                ('M', 'Male'),
     185                                ('F', 'Female'),
     186                        )
     187                        name = models.CharField(max_length=60)
     188                        gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
     189                       
     190                p = Person(name="Fred Flinstone", gender="M")
     191                p.save()
     192                p.gender               # Returns 'M'
     193                p.get_gender_display() # returns 'Male'
    174194
    175195    :attr:`~Field.default`
    176196        The default value for the field. This can be a value or a callable
Back to Top