summaryrefslogtreecommitdiff
path: root/tests/model_forms/test_modelchoicefield.py
diff options
context:
space:
mode:
authorCharles Roelli <charles@aurox.ch>2026-05-05 09:24:59 +0000
committerCharles Roelli <charles@aurox.ch>2026-05-05 09:24:59 +0000
commitb3c1fd51b6537c122b8ead6aa9234685560866d8 (patch)
tree544c306118205eb54bbd1ab97d3ea5e863dfc913 /tests/model_forms/test_modelchoicefield.py
parent9f790ef1a0f356cf6342b5d57bbaeac35aed0d9f (diff)
Fixed #31295 -- Avoided Select widget triggering additional query when using ModelChoiceIterator.fix-31295
Diffstat (limited to 'tests/model_forms/test_modelchoicefield.py')
-rw-r--r--tests/model_forms/test_modelchoicefield.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py
index 40c625da7c..387caf0778 100644
--- a/tests/model_forms/test_modelchoicefield.py
+++ b/tests/model_forms/test_modelchoicefield.py
@@ -169,6 +169,22 @@ class ModelChoiceFieldTests(TestCase):
Category.objects.all().delete()
self.assertIs(bool(f.choices), False)
+ def test_empty_label_one_query(self):
+ class MyForm(forms.Form):
+ f = forms.ModelChoiceField(Category.objects.all(), empty_label=None)
+
+ form = MyForm()
+ with self.assertNumQueries(1):
+ form.as_ul()
+ # Ensure the query re-runs when re-rendering the form.
+ with self.assertNumQueries(1):
+ form.as_ul()
+
+ # Ensure the iterator stops after yielding its choices.
+ choice_iterator = iter(form.fields["f"].choices)
+ self.assertEqual(len(list(choice_iterator)), 3)
+ self.assertEqual(len(list(choice_iterator)), 0)
+
def test_choices_bool_empty_label(self):
f = forms.ModelChoiceField(Category.objects.all(), empty_label="--------")
Category.objects.all().delete()