summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2017-03-15 12:54:26 -0400
committerGitHub <noreply@github.com>2017-03-15 12:54:26 -0400
commit44f9241c48e28823b140bc4ec7515f5a88b88c32 (patch)
treed2a0d165441b8ff530261b7712f198cd93ee9eeb
parent0d83052e52ddfe99c214be7ffca348a3c8feaef5 (diff)
Refs #27563 -- Fixed ModelChoiceField.__deepcopy__() so forms don't share a queryset cache.
Thanks Luke Benstead for the report Simon Charettes for the fix.
-rw-r--r--django/forms/models.py2
-rw-r--r--tests/model_forms/tests.py9
2 files changed, 10 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index a42ca4fb6e..895ebbd59d 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -1155,7 +1155,7 @@ class ModelChoiceField(ChoiceField):
def __deepcopy__(self, memo):
result = super(ChoiceField, self).__deepcopy__(memo)
# Need to force a new ModelChoiceIterator to be created, bug #11183
- result.queryset = result.queryset
+ result.queryset = self.queryset.all()
return result
def _get_queryset(self):
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index b92ba1fa4a..bcde7a868c 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -1631,6 +1631,15 @@ class ModelChoiceFieldTests(TestCase):
self.assertIsNot(field1, ModelChoiceForm.base_fields['category'])
self.assertIs(field1.widget.choices.field, field1)
+ def test_modelchoicefield_result_cache_not_shared(self):
+ class ModelChoiceForm(forms.Form):
+ category = forms.ModelChoiceField(Category.objects.all())
+
+ form1 = ModelChoiceForm()
+ self.assertCountEqual(form1.fields['category'].queryset, [self.c1, self.c2, self.c3])
+ form2 = ModelChoiceForm()
+ self.assertIsNone(form2.fields['category'].queryset._result_cache)
+
def test_modelchoicefield_22745(self):
"""
#22745 -- Make sure that ModelChoiceField with RadioSelect widget