summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Hill <alex@hill.net.au>2016-08-03 11:12:06 +0800
committerTim Graham <timograham@gmail.com>2016-08-03 10:53:42 -0400
commit86ae2b22ae2b0e11a4f20785afd5c475a94e59a7 (patch)
tree589e4de5dd233173ecd540c79319a7910949ce51
parenta8b8ef114dd01d9c1e71a620b732b9aac7b7da5c (diff)
[1.10.x] Fixed #27001 -- Fixed a query count regression in ModelChoiceField with RadioSelect.
Backport of c5ebfda00226e3695cadbc13ea9ce4c5951d3ed0 from master
-rw-r--r--django/forms/widgets.py7
-rw-r--r--docs/releases/1.10.1.txt3
-rw-r--r--tests/model_forms/tests.py8
3 files changed, 16 insertions, 2 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index 217f37b022..ec076398cc 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -693,8 +693,11 @@ class ChoiceFieldRenderer(object):
self.choices = choices
def __getitem__(self, idx):
- choice = list(self.choices)[idx] # Let the IndexError propagate
- return self.choice_input_class(self.name, self.value, self.attrs.copy(), choice, idx)
+ return list(self)[idx]
+
+ def __iter__(self):
+ for idx, choice in enumerate(self.choices):
+ yield self.choice_input_class(self.name, self.value, self.attrs.copy(), choice, idx)
def __str__(self):
return self.render()
diff --git a/docs/releases/1.10.1.txt b/docs/releases/1.10.1.txt
index 66ff4707f7..58619492a7 100644
--- a/docs/releases/1.10.1.txt
+++ b/docs/releases/1.10.1.txt
@@ -20,3 +20,6 @@ Bugfixes
* Fixed a checks framework crash with an empty ``Meta.default_permissions``
(:ticket:`26997`).
+
+* Fixed a regression in the number of queries when using ``RadioSelect`` with a
+ ``ModelChoiceField`` form field (:ticket:`27001`).
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 0b0390707e..f03c80fb6b 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -1539,6 +1539,14 @@ class ModelChoiceFieldTests(TestCase):
self.assertEqual(form.errors, {})
self.assertEqual([x.pk for x in form.cleaned_data['categories']], [category1.pk])
+ def test_radioselect_num_queries(self):
+ class CategoriesForm(forms.Form):
+ categories = forms.ModelChoiceField(Category.objects.all(), widget=forms.RadioSelect)
+
+ template = Template('{% for widget in form.categories %}{{ widget }}{% endfor %}')
+ with self.assertNumQueries(2):
+ template.render(Context({'form': CategoriesForm()}))
+
class ModelMultipleChoiceFieldTests(TestCase):
def setUp(self):