I've not got far in looking for a solution, but this test passes in 1.9
and fails in 1.10 and master with `AssertionError: 11 != 2 : 11 queries
executed, 2 expected`:
{{{#!python
def test_radioselect_num_queries(self):
class CategoriesForm(forms.Form):
categories = forms.ModelChoiceField(
queryset=Category.objects.all(),
widget=forms.RadioSelect
)
template = Template('{% for w in form.categories %}{{ w }}{%
endfor %}')
with self.assertNumQueries(2):
template.render(Context({'form': CategoriesForm()}))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27001>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* component: Uncategorized => Forms
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:1>
Comment (by AlexHill):
Branch with failing test here:
https://github.com/django/django/compare/master...AlexHill:ticket_27001
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:2>
Comment (by AlexHill):
Created a pull request with a fix.
`ChoiceFieldRenderer` doesn't have `__iter__` defined, so Python iterates
over it by calling `__getitem__` with an increasing index until an
exception is raised. `ChoiceFieldRenderer.__getitem__` calls `list` on
itself, which turns iteration into an n^2 operation. When the choices are
backed by a queryset as in ModelChoiceField, that means lots of redundant
database queries.
Fixed by adding an `__iter__` method to `ChoiceFieldRenderer` and changing
`__getitem__` to use it, so that indexing still works.
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:3>
* has_patch: 0 => 1
* type: Uncategorized => Bug
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:4>
* stage: Unreviewed => Accepted
Comment:
[https://github.com/django/django/pull/7011 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:5>
* severity: Normal => Release blocker
* needs_tests: 1 => 0
Comment:
As this is a regression in 1.10 I suppose this should be backported.
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:6>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"c5ebfda00226e3695cadbc13ea9ce4c5951d3ed0" c5ebfda]:
{{{
#!CommitTicketReference repository=""
revision="c5ebfda00226e3695cadbc13ea9ce4c5951d3ed0"
Fixed #27001 -- Fixed a query count regression in ModelChoiceField with
RadioSelect.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"86ae2b22ae2b0e11a4f20785afd5c475a94e59a7" 86ae2b2]:
{{{
#!CommitTicketReference repository=""
revision="86ae2b22ae2b0e11a4f20785afd5c475a94e59a7"
[1.10.x] Fixed #27001 -- Fixed a query count regression in
ModelChoiceField with RadioSelect.
Backport of c5ebfda00226e3695cadbc13ea9ce4c5951d3ed0 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:8>