summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorFrançois Freitag <mail@franek.fr>2018-04-13 18:39:10 -0700
committerTim Graham <timograham@gmail.com>2018-04-23 13:03:07 -0400
commitd1413c5d703c60dfb9e2a418c79b3e4aed32ffac (patch)
tree59f5a13de1561e5b4930cae91aee9f163a40d076 /django
parent3fca95e1ad5b355f7813b98c97a194a30f2ab47b (diff)
downloaddjango-d1413c5d703c60dfb9e2a418c79b3e4aed32ffac.tar.gz
Refs #28312 -- Added an optimized __bool__() to ModelChoiceIterator.
COUNT is more expensive than EXISTS; use the latter when possible.
Diffstat (limited to 'django')
-rw-r--r--django/forms/models.py3
1 files changed, 3 insertions, 0 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 67006ba390..cf66f10c88 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))