Ticket #18062: ref_models_fields_apr-13-2012.diff

File ref_models_fields_apr-13-2012.diff, 1.3 KB (added by danols, 3 years ago)

More direct to the point change

  • docs/ref/models/fields.txt

     
    100100The first element in each tuple is the actual value to be stored. The second
    101101element is the human-readable name for the option.
    102102
    103 The choices list can be defined either as part of your model class::
     103The choices list can be defined as part of your model class::
    104104
    105105    class Foo(models.Model):
     106        GENDER_MALE = 'M'
     107        GENDER_FEMALE = 'F'
    106108        GENDER_CHOICES = (
    107             ('M', 'Male'),
    108             ('F', 'Female'),
     109            (GENDER_MALE, 'Male'),
     110            (GENDER_FEMALE, 'Female'),
    109111        )
    110112        gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
     113       
     114        def greet(self):
     115                return {self.GENDER_MALE: 'Hi, boy', self.GENDER_FEMALE: 'Hi, girl.'}[self.gender]
    111116
    112 or outside your model class altogether::
    113 
    114     GENDER_CHOICES = (
    115         ('M', 'Male'),
    116         ('F', 'Female'),
    117     )
    118     class Foo(models.Model):
    119         gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    120 
    121117You can also collect your available choices into named groups that can
    122118be used for organizational purposes::
    123119
Back to Top