Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #16409 -- Fixed an error condition when using QuerySet only()/d…

…efer() 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
  • Loading branch information...
commit b2050ff546da4164f90a795e55d7d8c55981783d 1 parent f5c9c22
@ramiro ramiro authored
View
6 django/db/models/query.py
@@ -231,9 +231,6 @@ def iterator(self):
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 @@ def iterator(self):
# 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
View
2  django/db/models/sql/compiler.py
@@ -169,7 +169,7 @@ def get_columns(self, with_aliases=False):
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:
View
5 tests/regressiontests/defer_regress/tests.py
@@ -4,6 +4,7 @@
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 @@ def test():
]
)
+ # 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)
Please sign in to comment.
Something went wrong with that request. Please try again.