diff --git a/django/db/models/query.py b/django/db/models/query.py index 952739e939..b46528599e 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -102,7 +102,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() @@ -337,6 +337,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) diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py index db8c079962..1dd1176d8b 100644 --- a/tests/modeltests/basic/tests.py +++ b/tests/modeltests/basic/tests.py @@ -2,7 +2,7 @@ from __future__ import absolute_import, unicode_literals from datetime import datetime -from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned +from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError from django.db.models.fields import Field, FieldDoesNotExist from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature from django.utils import six @@ -639,3 +639,8 @@ class ModelTest(TestCase): Article.objects.bulk_create([Article(headline=lazy, pub_date=datetime.now())]) article = Article.objects.get() self.assertEqual(article.headline, notlazy) + + def test_invalid_qs_list(self): + qs = Article.objects.order_by('invalid_column') + self.assertRaises(FieldError, list, qs) + self.assertRaises(FieldError, list, qs) \ No newline at end of file