diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index ed21ea7..d1b22d0 100644
a
|
b
|
class BaseQuery(object):
|
1194 | 1194 | |
1195 | 1195 | elif (len(field_list) > 1 or |
1196 | 1196 | field_list[0] not in [i.name for i in opts.fields]): |
| 1197 | |
| 1198 | join_alias = self.get_initial_alias() |
| 1199 | |
1197 | 1200 | field, target, opts, join_list, last, _ = self.setup_joins( |
1198 | | field_list, opts, self.get_initial_alias(), False) |
| 1201 | field_list, opts, join_alias, False) |
1199 | 1202 | # Aggregate references a model or field that requires a join |
1200 | 1203 | self.allow_nulls = True |
1201 | | |
1202 | | col = (join_list[-1], target.column) |
| 1204 | |
| 1205 | final = len(join_list) |
| 1206 | penultimate = last.pop() |
| 1207 | if penultimate == final: |
| 1208 | penultimate = last.pop() |
| 1209 | join_alias = join_list[-1] |
| 1210 | |
| 1211 | col = target.column |
| 1212 | |
| 1213 | while final > 1: |
| 1214 | join = self.alias_map[join_alias] |
| 1215 | if col != join[RHS_JOIN_COL]: |
| 1216 | break |
| 1217 | self.unref_alias(join_alias) |
| 1218 | join_alias = join[LHS_ALIAS] |
| 1219 | col = join[LHS_JOIN_COL] |
| 1220 | join_list = join_list[:-1] |
| 1221 | final -= 1 |
| 1222 | if final == penultimate: |
| 1223 | penultimate = last.pop() |
| 1224 | |
| 1225 | col = (join_list[-1], col) |
1203 | 1226 | aggregate = aggregate_expr.add_to_query(self, aggregates, |
1204 | 1227 | col=col, |
1205 | 1228 | source=target, |