Fixed #14700 -- ensure that a raw query is only executed once per iteration.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14785 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-03 20:25:59 +00:00
parent 5475da1c32
commit d68598e022
2 changed files with 11 additions and 2 deletions

View File

@ -1310,9 +1310,13 @@ class RawQuerySet(object):
# Cache some things for performance reasons outside the loop. # Cache some things for performance reasons outside the loop.
db = self.db 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') need_resolv_columns = hasattr(compiler, 'resolve_columns')
query = iter(self.query)
# Find out which columns are model's fields, and which ones should be # Find out which columns are model's fields, and which ones should be
# annotated to the model. # annotated to the model.
for pos, column in enumerate(self.columns): for pos, column in enumerate(self.columns):
@ -1341,7 +1345,7 @@ class RawQuerySet(object):
if need_resolv_columns: if need_resolv_columns:
fields = [self.model_fields.get(c, None) for c in self.columns] fields = [self.model_fields.get(c, None) for c in self.columns]
# Begin looping through the query values. # Begin looping through the query values.
for values in self.query: for values in query:
if need_resolv_columns: if need_resolv_columns:
values = compiler.resolve_columns(values, fields) values = compiler.resolve_columns(values, fields)
# Associate fields to values # Associate fields to values

View File

@ -217,3 +217,8 @@ class RawQueryTests(TestCase):
self.assertEqual( self.assertEqual(
[o.pk for o in FriendlyAuthor.objects.raw(query)], [f.pk] [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")
)