Code

Ticket #11183: 11183.2.diff

File 11183.2.diff, 1.8 KB (added by lukeplant, 4 years ago)

revised patch

Line 
1Index: django/forms/models.py
2===================================================================
3--- django/forms/models.py      (revision 12718)
4+++ django/forms/models.py      (working copy)
5@@ -923,6 +923,12 @@
6         self.choice_cache = None
7         self.to_field_name = to_field_name
8 
9+    def __deepcopy__(self, memo):
10+        result = super(ChoiceField, self).__deepcopy__(memo)
11+        # Need to force a new ModelChoiceIterator to be created, bug #11183
12+        result.queryset = result.queryset
13+        return result
14+
15     def _get_queryset(self):
16         return self._queryset
17 
18Index: tests/regressiontests/model_forms_regress/tests.py
19===================================================================
20--- tests/regressiontests/model_forms_regress/tests.py  (revision 12718)
21+++ tests/regressiontests/model_forms_regress/tests.py  (working copy)
22@@ -2,7 +2,7 @@
23 
24 from django import db
25 from django import forms
26-from django.forms.models import modelform_factory
27+from django.forms.models import modelform_factory, ModelChoiceField
28 from django.conf import settings
29 from django.test import TestCase
30 
31@@ -203,3 +203,16 @@
32         form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
33         self.assert_(not form.is_valid())
34 
35+
36+class ModelChoiceForm1(forms.Form):
37+    person = ModelChoiceField(Person.objects.all())
38+
39+
40+class TestTicket11183(TestCase):
41+    def test_11183(self):
42+        form1 = ModelChoiceForm1()
43+        field1 = form1.fields['person']
44+        self.assert_(field1 is not ModelChoiceForm1.base_fields['person']) # sanity check
45+        # To allow us to change the queryset of field1.widget.choices correctly,
46+        # without affecting other forms, the following must hold:
47+        self.assert_(field1.widget.choices.field is not ModelChoiceForm1.base_fields['person'])