Ticket #18062: ref_models_fields_choices.diff

File ref_models_fields_choices.diff, 862 bytes (added by Thomas Güttler, 12 years ago)

fixed typo: in method self.GENDER_.. is needed

  • docs/ref/models/fields.txt

     
    118118    class Foo(models.Model):
    119119        gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    120120
     121To avoid typos the keys of the choices can be attributes of the class::
     122
     123    class User(models.Model):
     124        GENDER_MALE = 0
     125        GENDER_FEMALE = 1
     126        GENDER_CHOICES = [(GENDER_MALE, 'Male'), (GENDER_FEMALE, 'Female')]
     127        gender = models.IntegerField(choices=GENDER_CHOICES)
     128   
     129        def greet(self):
     130            return {self.GENDER_MALE: 'Hi, boy', self.GENDER_FEMALE: 'Hi, girl.'}[self.gender]
     131
    121132You can also collect your available choices into named groups that can
    122133be used for organizational purposes::
    123134
Back to Top