Skip to content

Commit 3951680

Browse files
committed
Fixed #10064 -- Corrected handling of aggregate queries that also use select_related(). Thanks to olivius for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9781 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 7281088 commit 3951680

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

django/db/models/sql/query.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,12 @@ def results_iter(self):
250250

251251
if self.aggregate_select:
252252
aggregate_start = len(self.extra_select.keys()) + len(self.select)
253+
aggregate_end = aggregate_start + len(self.aggregate_select)
253254
row = tuple(row[:aggregate_start]) + tuple([
254255
self.resolve_aggregate(value, aggregate)
255256
for (alias, aggregate), value
256-
in zip(self.aggregate_select.items(), row[aggregate_start:])
257-
])
257+
in zip(self.aggregate_select.items(), row[aggregate_start:aggregate_end])
258+
]) + tuple(row[aggregate_end:])
258259

259260
yield row
260261

tests/regressiontests/aggregation_regress/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def __unicode__(self):
147147
>>> Book.objects.aggregate(number=Max('pages'), select=Max('pages'))
148148
{'number': 1132, 'select': 1132}
149149
150+
# Regression for #10064: select_related() plays nice with aggregates
151+
>>> Book.objects.select_related('publisher').annotate(num_authors=Count('authors')).values()[0]
152+
{'rating': 4.0, 'isbn': u'013790395', 'name': u'Artificial Intelligence: A Modern Approach', 'pubdate': datetime.date(1995, 1, 15), 'price': Decimal("82.8..."), 'id': 5, 'num_authors': 2, 'publisher_id': 3, 'pages': 1132}
150153
151154
"""
152155
}

0 commit comments

Comments
 (0)