diff --git a/django/db/models/query.py b/django/db/models/query.py
index 0f3a79a..817f518 100644
a
|
b
|
class QuerySet(object):
|
104 | 104 | len(self) |
105 | 105 | |
106 | 106 | if self._result_cache is None: |
107 | | self._iter = self.iterator() |
| 107 | self._iter = self.safe_iterator(self.iterator()) |
108 | 108 | self._result_cache = [] |
109 | 109 | if self._iter: |
110 | 110 | return self._result_iter() |
… |
… |
class QuerySet(object):
|
341 | 341 | |
342 | 342 | yield obj |
343 | 343 | |
| 344 | def safe_iterator(self, iterator): |
| 345 | # ensure result cache is cleared when iterating over a queryset |
| 346 | # raises an exception |
| 347 | try: |
| 348 | for item in iterator: |
| 349 | yield item |
| 350 | except StopIteration: |
| 351 | raise |
| 352 | except Exception: |
| 353 | self._result_cache = None |
| 354 | raise |
| 355 | |
344 | 356 | def aggregate(self, *args, **kwargs): |
345 | 357 | """ |
346 | 358 | Returns a dictionary containing the calculations (aggregation) |