mirror of
https://github.com/django/django.git
synced 2025-10-29 08:36:09 +00:00
Fixed 19895 -- Made second iteration over invalid queryset raise an exception too
When iteration over a queryset raised an exception, the result cache remained initialized with an empty list, so subsequent iterations returned an empty list instead of raising an exception
This commit is contained in:
committed by
Jacob Kaplan-Moss
parent
4c05fdb467
commit
2cd0edaa47
@@ -104,7 +104,7 @@ class QuerySet(object):
|
||||
len(self)
|
||||
|
||||
if self._result_cache is None:
|
||||
self._iter = self.iterator()
|
||||
self._iter = self._safe_iterator(self.iterator())
|
||||
self._result_cache = []
|
||||
if self._iter:
|
||||
return self._result_iter()
|
||||
@@ -341,6 +341,18 @@ class QuerySet(object):
|
||||
|
||||
yield obj
|
||||
|
||||
def _safe_iterator(self, iterator):
|
||||
# ensure result cache is cleared when iterating over a queryset
|
||||
# raises an exception
|
||||
try:
|
||||
for item in iterator:
|
||||
yield item
|
||||
except StopIteration:
|
||||
raise
|
||||
except Exception:
|
||||
self._result_cache = None
|
||||
raise
|
||||
|
||||
def aggregate(self, *args, **kwargs):
|
||||
"""
|
||||
Returns a dictionary containing the calculations (aggregation)
|
||||
|
||||
Reference in New Issue
Block a user