mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Refs #28312 -- Added an optimized __bool__() to ModelChoiceIterator.
COUNT is more expensive than EXISTS; use the latter when possible.
This commit is contained in:
committed by
Tim Graham
parent
3fca95e1ad
commit
d1413c5d70
@@ -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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user