Ticket #19895: 19895_2.diff

File 19895_2.diff, 1.0 KB (added by gnosek, 2 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 0f3a79a..817f518 100644
    a b class QuerySet(object): 
    104104            len(self)
    105105
    106106        if self._result_cache is None:
    107             self._iter = self.iterator()
     107            self._iter = self.safe_iterator(self.iterator())
    108108            self._result_cache = []
    109109        if self._iter:
    110110            return self._result_iter()
    class QuerySet(object): 
    341341
    342342            yield obj
    343343
     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
    344356    def aggregate(self, *args, **kwargs):
    345357        """
    346358        Returns a dictionary containing the calculations (aggregation)
Back to Top