mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #27002 -- Prevented double query when rendering ModelChoiceField.
This commit is contained in:
parent
29a3f8b4bb
commit
74105b2636
2 changed files with 28 additions and 12 deletions
|
@ -1576,11 +1576,22 @@ class ModelChoiceFieldTests(TestCase):
|
|||
field = CustomModelChoiceField(Category.objects.all())
|
||||
self.assertIsInstance(field.choices, CustomModelChoiceIterator)
|
||||
|
||||
def test_radioselect_num_queries(self):
|
||||
class CategoriesForm(forms.Form):
|
||||
categories = forms.ModelChoiceField(Category.objects.all(), widget=forms.RadioSelect)
|
||||
def test_modelchoicefield_num_queries(self):
|
||||
"""
|
||||
Widgets that render multiple subwidgets shouldn't make more than one
|
||||
database query.
|
||||
"""
|
||||
categories = Category.objects.all()
|
||||
|
||||
class CategoriesForm(forms.Form):
|
||||
radio = forms.ModelChoiceField(queryset=categories, widget=forms.RadioSelect)
|
||||
checkbox = forms.ModelMultipleChoiceField(queryset=categories, widget=forms.CheckboxSelectMultiple)
|
||||
|
||||
template = Template("""
|
||||
{% for widget in form.checkbox %}{{ widget }}{% endfor %}
|
||||
{% for widget in form.radio %}{{ widget }}{% endfor %}
|
||||
""")
|
||||
|
||||
template = Template('{% for widget in form.categories %}{{ widget }}{% endfor %}')
|
||||
with self.assertNumQueries(2):
|
||||
template.render(Context({'form': CategoriesForm()}))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue