Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
  • Loading branch information...
commit e0d68084a0a89ada1ff030d03d9f9eb51dcfd159 1 parent 99b5526
Alex Gaynor authored December 03, 2010
8  django/db/models/query.py
@@ -1398,9 +1398,13 @@ def __iter__(self):
1398 1398
 
1399 1399
         # Cache some things for performance reasons outside the loop.
1400 1400
         db = self.db
1401  
-        compiler = connections[db].ops.compiler('SQLCompiler')(self.query, connections[db], db)
  1401
+        compiler = connections[db].ops.compiler('SQLCompiler')(
  1402
+            self.query, connections[db], db
  1403
+        )
1402 1404
         need_resolv_columns = hasattr(compiler, 'resolve_columns')
1403 1405
 
  1406
+        query = iter(self.query)
  1407
+
1404 1408
         # Find out which columns are model's fields, and which ones should be
1405 1409
         # annotated to the model.
1406 1410
         for pos, column in enumerate(self.columns):
@@ -1429,7 +1433,7 @@ def __iter__(self):
1429 1433
         if need_resolv_columns:
1430 1434
             fields = [self.model_fields.get(c, None) for c in self.columns]
1431 1435
         # Begin looping through the query values.
1432  
-        for values in self.query:
  1436
+        for values in query:
1433 1437
             if need_resolv_columns:
1434 1438
                 values = compiler.resolve_columns(values, fields)
1435 1439
             # Associate fields to values
5  tests/modeltests/raw_query/tests.py
@@ -217,3 +217,8 @@ def test_inheritance(self):
217 217
         self.assertEqual(
218 218
             [o.pk for o in FriendlyAuthor.objects.raw(query)], [f.pk]
219 219
         )
  220
+
  221
+    def test_query_count(self):
  222
+        self.assertNumQueries(1,
  223
+            list, Author.objects.raw("SELECT * FROM raw_query_author")
  224
+        )

0 notes on commit e0d6808

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