summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRamiro Morales <cramm0@gmail.com>2011-07-07 01:12:45 +0000
committerRamiro Morales <cramm0@gmail.com>2011-07-07 01:12:45 +0000
commitb2050ff546da4164f90a795e55d7d8c55981783d (patch)
tree73e8673db5936f671cebaf8870c39e8242aa8fa5
parentf5c9c2246e4b01f8731550c2a10241ca0e0148e9 (diff)
Fixed #16409 -- Fixed an error condition when using QuerySet only()/defer() on the result of an annotate() call. Thanks jaklaassen AT gmail DOT com and Tai Lee for the reports and Tai for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16522 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/query.py6
-rw-r--r--django/db/models/sql/compiler.py2
-rw-r--r--tests/regressiontests/defer_regress/tests.py5
3 files changed, 9 insertions, 4 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 6a6a82968f..57288f42dd 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -231,9 +231,6 @@ class QuerySet(object):
fields = self.model._meta.fields
pk_idx = self.model._meta.pk_index()
- index_start = len(extra_select)
- aggregate_start = index_start + len(self.model._meta.fields)
-
load_fields = []
# If only/defer clauses have been specified,
# build the list of fields that are to be loaded.
@@ -253,6 +250,9 @@ class QuerySet(object):
# Therefore, we need to load all fields from this model
load_fields.append(field.name)
+ index_start = len(extra_select)
+ aggregate_start = index_start + len(load_fields or self.model._meta.fields)
+
skip = None
if load_fields and not fill_cache:
# Some fields have been deferred, so we have to initialise
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 841ec12f2d..b5f170eb49 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -169,7 +169,7 @@ class SQLCompiler(object):
if isinstance(col, (list, tuple)):
alias, column = col
table = self.query.alias_map[alias][TABLE_NAME]
- if table in only_load and col not in only_load[table]:
+ if table in only_load and column not in only_load[table]:
continue
r = '%s.%s' % (qn(alias), qn(column))
if with_aliases:
diff --git a/tests/regressiontests/defer_regress/tests.py b/tests/regressiontests/defer_regress/tests.py
index fbcf85e078..b1d88e3f36 100644
--- a/tests/regressiontests/defer_regress/tests.py
+++ b/tests/regressiontests/defer_regress/tests.py
@@ -4,6 +4,7 @@ from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.backends.db import SessionStore
from django.db import connection
+from django.db.models import Count
from django.db.models.loading import cache
from django.test import TestCase
@@ -148,6 +149,10 @@ class DeferRegressionTest(TestCase):
]
)
+ # Regression for #16409 - make sure defer() and only() work with annotate()
+ self.assertIsInstance(list(Item.objects.annotate(Count('relateditem')).defer('name')), list)
+ self.assertIsInstance(list(Item.objects.annotate(Count('relateditem')).only('name')), list)
+
def test_only_and_defer_usage_on_proxy_models(self):
# Regression for #15790 - only() broken for proxy models
proxy = Proxy.objects.create(name="proxy", value=42)