1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #29158 -- Fixed len(choices) crash if ModelChoiceField's queryset is a manager.

Removing all() in __iter__() prevents a duplicate query when choices are
cast to a list and there's a prefetch_related().
This commit is contained in:
François Freitag
2018-02-23 21:12:09 -08:00
committed by Tim Graham
parent 06172d7bc2
commit 40f0aa9885
3 changed files with 4 additions and 3 deletions

View File

@@ -1130,7 +1130,7 @@ class ModelChoiceIterator:
def __iter__(self):
if self.field.empty_label is not None:
yield ("", self.field.empty_label)
queryset = self.queryset.all()
queryset = self.queryset
# Can't use iterator() when queryset uses prefetch_related()
if not queryset._prefetch_related_lookups:
queryset = queryset.iterator()
@@ -1194,7 +1194,7 @@ class ModelChoiceField(ChoiceField):
return self._queryset
def _set_queryset(self, queryset):
self._queryset = queryset
self._queryset = None if queryset is None else queryset.all()
self.widget.choices = self.choices
queryset = property(_get_queryset, _set_queryset)