#27496: Iterating over ModelChoiceField with RadioSelect widget duplicates query
--------------------------------------+------------------------
Reporter: a-p-f | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
If you have a form with a ModelChoiceField using a RadioSelect widget and
iterate over the individual radio inputs in your template (as demonstrated
in the docs-
https://docs.djangoproject.com/en/1.10/ref/forms/widgets/#radioselect),
then the field's queryset will be evaluated for each option. Actually, the
query seems to be executed 2n + 5 times, where n is the number of objects
returned by the queryset.
To reproduce this, use the following code with any model and watch your
database server logs.
{{{#!python
class BugTestForm(forms.Form):
choice = forms.ModelChoiceField(queryset=MyModel.objects.all(),
widget=forms.RadioSelect)
form = BugTestForm()
list(form['choice'])
}}}
The same issue occurs when using ModelMultipleChoiceField with a
CheckboxSelectMultiple widget.
--
Ticket URL: <https://code.djangoproject.com/ticket/27496>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/048.34e1841b686525ec3d0248960e4fc04c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.