Ticket #18062: ref_models_fields_choices.diff
File ref_models_fields_choices.diff, 862 bytes (added by , 13 years ago) |
---|
-
docs/ref/models/fields.txt
118 118 class Foo(models.Model): 119 119 gender = models.CharField(max_length=1, choices=GENDER_CHOICES) 120 120 121 To 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 121 132 You can also collect your available choices into named groups that can 122 133 be used for organizational purposes:: 123 134