Ticket #18062: ref_models_fields_apr-13-2012.diff
File ref_models_fields_apr-13-2012.diff, 1.3 KB (added by , 13 years ago) |
---|
-
docs/ref/models/fields.txt
100 100 The first element in each tuple is the actual value to be stored. The second 101 101 element is the human-readable name for the option. 102 102 103 The choices list can be defined eitheras part of your model class::103 The choices list can be defined as part of your model class:: 104 104 105 105 class Foo(models.Model): 106 GENDER_MALE = 'M' 107 GENDER_FEMALE = 'F' 106 108 GENDER_CHOICES = ( 107 ( 'M', 'Male'),108 ( 'F', 'Female'),109 (GENDER_MALE, 'Male'), 110 (GENDER_FEMALE, 'Female'), 109 111 ) 110 112 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] 111 116 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 121 117 You can also collect your available choices into named groups that can 122 118 be used for organizational purposes:: 123 119