Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.5.x] Fixed #19895 -- Made second iteration over invalid queryset r…

…aise 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

Backport of 2cd0eda from master.
  • Loading branch information...
commit d1e87eb3baf75b1b6a0ada46a9b77f7e347cdb60 1 parent 2683e8e
@gnosek gnosek authored jacobian committed
Showing with 19 additions and 2 deletions.
  1. +13 −1 django/db/models/query.py
  2. +6 −1 tests/modeltests/basic/tests.py
View
14 django/db/models/query.py
@@ -102,7 +102,7 @@ def __iter__(self):
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 @@ def iterator(self):
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)
View
7 tests/modeltests/basic/tests.py
@@ -2,7 +2,7 @@
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 @@ def test_create_relation_with_ugettext_lazy(self):
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)

0 comments on commit d1e87eb

Please sign in to comment.
Something went wrong with that request. Please try again.