Code

Ticket #2037: 2037_2.diff

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

ForeignKeys didn't work as expected

Line 
1=== modified file 'django/db/models/fields/__init__.py'
2--- django/db/models/fields/__init__.py 2007-05-20 01:20:19 +0000
3+++ django/db/models/fields/__init__.py 2007-05-20 02:04:56 +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 or self.null) 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  2007-05-20 01:20:21 +0000
16+++ tests/modeltests/choices/models.py  2007-05-20 02:06:23 +0000
17@@ -23,6 +23,19 @@
18     def __str__(self):
19         return self.name
20 
21+CHOICES = (
22+    (0, 'State A'),
23+    (1, 'State B'),
24+)
25+
26+class ChoiceableModel(models.Model):
27+    emptynotallowed = models.CharField(maxlength=1, choices=CHOICES)
28+    emptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True)
29+    defaultemptynotallowed = models.CharField(maxlength=1, choices=CHOICES, default=2)
30+    defaultemptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True, default=2)
31+    nullrelation = models.ForeignKey(Person, null=True)
32+    relation = models.ForeignKey(Person, related_name='notnulrelation')
33+
34 __test__ = {'API_TESTS':"""
35 >>> a = Person(name='Adrian', gender='M')
36 >>> a.save()
37@@ -36,4 +49,16 @@
38 'Male'
39 >>> s.get_gender_display()
40 'Female'
41+>>> ChoiceableModel._meta.fields[1].get_choices()
42+[(0, 'State A'), (1, 'State B')]
43+>>> ChoiceableModel._meta.fields[2].get_choices()
44+[('', '---------'), (0, 'State A'), (1, 'State B')]
45+>>> ChoiceableModel._meta.fields[3].get_choices()
46+[(0, 'State A'), (1, 'State B')]
47+>>> ChoiceableModel._meta.fields[4].get_choices()
48+[('', '---------'), (0, 'State A'), (1, 'State B')]
49+>>> ChoiceableModel._meta.fields[5].get_choices()
50+[('', '---------'), (1, 'Adrian'), (2, 'Sara')]
51+>>> ChoiceableModel._meta.fields[6].get_choices()
52+[(1, 'Adrian'), (2, 'Sara')]
53 """}
54
55=== modified file 'tests/modeltests/manipulators/models.py'
56--- tests/modeltests/manipulators/models.py     2007-05-20 01:20:22 +0000
57+++ tests/modeltests/manipulators/models.py     2007-05-20 01:37:56 +0000
58@@ -54,7 +54,7 @@
59 
60 # Attempt to create an Album with an invalid musician.
61 >>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['foo']}))
62-{'musician': ["Select a valid choice; 'foo' is not in ['', '1']."]}
63+{'musician': ["Select a valid choice; 'foo' is not in ['1']."]}
64 
65 # Attempt to create an Album with an invalid release_date.
66 >>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['1'], 'release_date': 'today'}))
67