diff options
| author | Alex Gaynor <alex.gaynor@gmail.com> | 2010-12-03 20:26:57 +0000 |
|---|---|---|
| committer | Alex Gaynor <alex.gaynor@gmail.com> | 2010-12-03 20:26:57 +0000 |
| commit | e0d68084a0a89ada1ff030d03d9f9eb51dcfd159 (patch) | |
| tree | a357b358549827e3db292f81e7d72766a2b84f1d | |
| parent | 99b5526c19841abb398364947d04460f743dc216 (diff) | |
[1.2.X] Fixed #14700 -- ensure that a raw query is only executed once per iteration. Backport of [14785].
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14786 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/db/models/query.py | 8 | ||||
| -rw-r--r-- | tests/modeltests/raw_query/tests.py | 5 |
2 files changed, 11 insertions, 2 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index 920afd11c8..362114b535 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1398,9 +1398,13 @@ class RawQuerySet(object): # Cache some things for performance reasons outside the loop. db = self.db - compiler = connections[db].ops.compiler('SQLCompiler')(self.query, connections[db], db) + compiler = connections[db].ops.compiler('SQLCompiler')( + self.query, connections[db], db + ) need_resolv_columns = hasattr(compiler, 'resolve_columns') + query = iter(self.query) + # Find out which columns are model's fields, and which ones should be # annotated to the model. for pos, column in enumerate(self.columns): @@ -1429,7 +1433,7 @@ class RawQuerySet(object): if need_resolv_columns: fields = [self.model_fields.get(c, None) for c in self.columns] # Begin looping through the query values. - for values in self.query: + for values in query: if need_resolv_columns: values = compiler.resolve_columns(values, fields) # Associate fields to values diff --git a/tests/modeltests/raw_query/tests.py b/tests/modeltests/raw_query/tests.py index a0325eff97..e7cadfae8c 100644 --- a/tests/modeltests/raw_query/tests.py +++ b/tests/modeltests/raw_query/tests.py @@ -217,3 +217,8 @@ class RawQueryTests(TestCase): self.assertEqual( [o.pk for o in FriendlyAuthor.objects.raw(query)], [f.pk] ) + + def test_query_count(self): + self.assertNumQueries(1, + list, Author.objects.raw("SELECT * FROM raw_query_author") + ) |
