From d1413c5d703c60dfb9e2a418c79b3e4aed32ffac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Fri, 13 Apr 2018 18:39:10 -0700 Subject: [PATCH] Refs #28312 -- Added an optimized __bool__() to ModelChoiceIterator. COUNT is more expensive than EXISTS; use the latter when possible. --- django/forms/models.py | 3 +++ tests/model_forms/test_modelchoicefield.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/django/forms/models.py b/django/forms/models.py index 67006ba390b..cf66f10c88f 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1145,6 +1145,9 @@ class ModelChoiceIterator: # and __len__() won't be called. return self.queryset.count() + (1 if self.field.empty_label is not None else 0) + def __bool__(self): + return self.field.empty_label is not None or self.queryset.exists() + def choice(self, obj): return (self.field.prepare_value(obj), self.field.label_from_instance(obj)) diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py index 975873c7f94..adb3754382c 100644 --- a/tests/model_forms/test_modelchoicefield.py +++ b/tests/model_forms/test_modelchoicefield.py @@ -115,6 +115,17 @@ class ModelChoiceFieldTests(TestCase): (c4.pk, 'Fourth'), ]) + def test_choices_bool(self): + f = forms.ModelChoiceField(Category.objects.all(), empty_label=None) + self.assertIs(bool(f.choices), True) + Category.objects.all().delete() + self.assertIs(bool(f.choices), False) + + def test_choices_bool_empty_label(self): + f = forms.ModelChoiceField(Category.objects.all(), empty_label='--------') + Category.objects.all().delete() + self.assertIs(bool(f.choices), True) + def test_deepcopies_widget(self): class ModelChoiceForm(forms.Form): category = forms.ModelChoiceField(Category.objects.all())