Code

Ticket #2037: 2037.diff

File 2037.diff, 2.5 KB (added by Marc Fargas <telenieko@…>, 7 years ago)

Patching the behaviour of Field.get_choices() + tests

Line 
1=== modified file 'django/db/models/fields/__init__.py'
2--- django/db/models/fields/__init__.py 2007-05-01 02:35:15 +0000
3+++ django/db/models/fields/__init__.py 2007-05-20 01:29:25 +0000
4@@ -294,7 +294,7 @@
5 
6     def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
7         "Returns a list of tuples used as SelectField choices for this field."
8-        first_choice = include_blank and blank_choice or []
9+        first_choice = include_blank and self.blank and blank_choice or []
10         if self.choices:
11             return first_choice + list(self.choices)
12         rel_model = self.rel.to
13
14=== modified file 'tests/modeltests/choices/models.py'
15--- tests/modeltests/choices/models.py  2006-08-27 13:59:47 +0000
16+++ tests/modeltests/choices/models.py  2007-05-20 01:49:42 +0000
17@@ -23,6 +23,18 @@
18     def __str__(self):
19         return self.name
20 
21+CHOICES = (
22+    (0, 'State A'),
23+    (1, 'State B'),
24+)
25+
26+
27+class ChoiceableModel(models.Model):
28+    emptynotallowed = models.CharField(maxlength=1, choices=CHOICES)
29+    emptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True)
30+    defaultemptynotallowed = models.CharField(maxlength=1, choices=CHOICES, default=2)
31+    defaultemptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True, default=2)
32+
33 __test__ = {'API_TESTS':"""
34 >>> a = Person(name='Adrian', gender='M')
35 >>> a.save()
36@@ -36,4 +48,12 @@
37 'Male'
38 >>> s.get_gender_display()
39 'Female'
40+>>> ChoiceableModel._meta.fields[1].get_choices()
41+[(0, 'State A'), (1, 'State B')]
42+>>> ChoiceableModel._meta.fields[2].get_choices()
43+[('', '---------'), (0, 'State A'), (1, 'State B')]
44+>>> ChoiceableModel._meta.fields[3].get_choices()
45+[(0, 'State A'), (1, 'State B')]
46+>>> ChoiceableModel._meta.fields[4].get_choices()
47+[('', '---------'), (0, 'State A'), (1, 'State B')]
48 """}
49
50=== modified file 'tests/modeltests/manipulators/models.py'
51--- tests/modeltests/manipulators/models.py     2007-03-23 20:17:04 +0000
52+++ tests/modeltests/manipulators/models.py     2007-05-20 01:37:56 +0000
53@@ -54,7 +54,7 @@
54 
55 # Attempt to create an Album with an invalid musician.
56 >>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['foo']}))
57-{'musician': ["Select a valid choice; 'foo' is not in ['', '1']."]}
58+{'musician': ["Select a valid choice; 'foo' is not in ['1']."]}
59 
60 # Attempt to create an Album with an invalid release_date.
61 >>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['1'], 'release_date': 'today'}))
62