Code

Ticket #18062: ref_models_fields_apr-13-2012.diff

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

More direct to the point change

Line 
1Index: docs/ref/models/fields.txt
2===================================================================
3--- docs/ref/models/fields.txt  (revision 17906)
4+++ docs/ref/models/fields.txt  (working copy)
5@@ -100,24 +100,20 @@
6 The first element in each tuple is the actual value to be stored. The second
7 element is the human-readable name for the option.
8 
9-The choices list can be defined either as part of your model class::
10+The choices list can be defined as part of your model class::
11 
12     class Foo(models.Model):
13+        GENDER_MALE = 'M'
14+        GENDER_FEMALE = 'F'
15         GENDER_CHOICES = (
16-            ('M', 'Male'),
17-            ('F', 'Female'),
18+            (GENDER_MALE, 'Male'),
19+            (GENDER_FEMALE, 'Female'),
20         )
21         gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
22+       
23+        def greet(self):
24+               return {self.GENDER_MALE: 'Hi, boy', self.GENDER_FEMALE: 'Hi, girl.'}[self.gender]
25 
26-or outside your model class altogether::
27-
28-    GENDER_CHOICES = (
29-        ('M', 'Male'),
30-        ('F', 'Female'),
31-    )
32-    class Foo(models.Model):
33-        gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
34-
35 You can also collect your available choices into named groups that can
36 be used for organizational purposes::
37