The problem manifests as annotations being assigned an incorrect value
when there are deferred fields and select_related is in use.
Passing Test:
{{{
def test_annotate_defer(self):
qs = Book.objects.annotate(
page_sum=Sum("pages")).defer('name').filter(pk=1)
rows = [
(1, "159059725", 447, "The Definitive Guide to Django: Web
Development Done Right")
]
self.assertQuerysetEqual(
qs.order_by('pk'), rows,
lambda r: (r.id, r.isbn, r.page_sum, r.name)
)
}}}
Failing Test:
{{{
def test_annotate_defer_select_related(self):
qs = Book.objects.select_related('contact').annotate(
page_sum=Sum("pages")).defer('name').filter(pk=1)
rows = [
(1, "159059725", 447, "Adrian Holovaty",
"The Definitive Guide to Django: Web Development Done Right")
]
self.assertQuerysetEqual(
qs.order_by('pk'), rows,
lambda r: (r.id, r.isbn, r.page_sum, r.contact.name, r.name)
)
}}}
The problem is in the iterator method of django.db.models.query, which
results in the aggregate_start variable being off by len(deferred):
{{{
if load_fields and not fill_cache:
# Some fields have been deferred, so we have to initialize
# via keyword arguments.
skip = set()
init_list = []
for field in fields:
if field.name not in load_fields:
skip.add(field.attname)
else:
init_list.append(field.attname)
model_cls = deferred_class_factory(self.model, skip)
else:
model_cls = self.model
init_list = [f.attname for f in fields]
}}}
fill_cache is set to true when select_related is in use, which skips to
the else block. The else block doesn't take deferred fields into account
like the if block does. I'll attempt to patch this by including similar
logic in the else block (skipping deferred fields), but I'm not yet sure
what else that might affect.
--
Ticket URL: <https://code.djangoproject.com/ticket/23001>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Both tests pass on the 1.7 branch.
--
Ticket URL: <https://code.djangoproject.com/ticket/23001#comment:1>
* has_patch: 0 => 1
Comment:
https://github.com/django/django/pull/2903
--
Ticket URL: <https://code.djangoproject.com/ticket/23001#comment:2>
* type: Uncategorized => Bug
* stage: Unreviewed => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/23001#comment:3>
* owner: smeatonj => jarshwah
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/23001#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"5b0375ec3e7473fcc29c21003fef80c0d0be183f"]:
{{{
#!CommitTicketReference repository=""
revision="5b0375ec3e7473fcc29c21003fef80c0d0be183f"
Fixed #23001 -- Fixed mixing defer and annotations
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23001#comment:5>