[Django] #27001: Regression in query counts using RadioSelect with ModelChoiceField

17 views
Skip to first unread message

Django

unread,
Aug 2, 2016, 10:11:37 PM8/2/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
-------------------------------+--------------------
Reporter: AlexHill | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Before 1.9, I had on my list of things to look at the fact that a standard
ModelChoiceField with a RadioSelect takes two queries. Now looking at this
in 1.10, the number of queries has ballooned (to 11 queries in my simple
test case).

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.

Django

unread,
Aug 2, 2016, 10:15:39 PM8/2/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
-------------------------------+--------------------------------------

Reporter: AlexHill | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by AlexHill):

* needs_better_patch: => 0
* component: Uncategorized => Forms
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:1>

Django

unread,
Aug 2, 2016, 10:16:49 PM8/2/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
-------------------------------+--------------------------------------

Reporter: AlexHill | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

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>

Django

unread,
Aug 2, 2016, 11:33:39 PM8/2/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
-------------------------------+--------------------------------------

Reporter: AlexHill | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

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>

Django

unread,
Aug 2, 2016, 11:35:16 PM8/2/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
--------------------------+--------------------------------------
Reporter: AlexHill | Owner: nobody
Type: Bug | Status: new

Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------+--------------------------------------
Changes (by AlexHill):

* has_patch: 0 => 1
* type: Uncategorized => Bug
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:4>

Django

unread,
Aug 2, 2016, 11:37:28 PM8/2/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
--------------------------+------------------------------------
Reporter: AlexHill | Owner: nobody

Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------+------------------------------------
Changes (by charettes):

* stage: Unreviewed => Accepted


Comment:

[https://github.com/django/django/pull/7011 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27001#comment:5>

Django

unread,
Aug 2, 2016, 11:39:14 PM8/2/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
---------------------------------+------------------------------------
Reporter: AlexHill | Owner: nobody

Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Release blocker | Resolution:

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by charettes):

* 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>

Django

unread,
Aug 3, 2016, 10:53:26 AM8/3/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
---------------------------------+------------------------------------
Reporter: AlexHill | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Django

unread,
Aug 3, 2016, 11:07:41 AM8/3/16
to django-...@googlegroups.com
#27001: Regression in query counts using RadioSelect with ModelChoiceField
---------------------------------+------------------------------------
Reporter: AlexHill | Owner: nobody

Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages