diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index ed21ea7..8d69966 100644
a
|
b
|
class BaseQuery(object):
|
1198 | 1198 | field_list, opts, self.get_initial_alias(), False) |
1199 | 1199 | # Aggregate references a model or field that requires a join |
1200 | 1200 | self.allow_nulls = True |
1201 | | |
1202 | | col = (join_list[-1], target.column) |
| 1201 | |
| 1202 | if hasattr(target, 'column'): |
| 1203 | col = (join_list[-1], target.column) |
| 1204 | else: |
| 1205 | col = (join_list[-1], target) |
1203 | 1206 | aggregate = aggregate_expr.add_to_query(self, aggregates, |
1204 | 1207 | col=col, |
1205 | 1208 | source=target, |
… |
… |
class BaseQuery(object):
|
1302 | 1305 | for alias in extra: |
1303 | 1306 | self.unref_alias(alias) |
1304 | 1307 | else: |
1305 | | col = target.column |
| 1308 | if hasattr(target, 'column'): |
| 1309 | col = target.column |
| 1310 | else: |
| 1311 | col = target |
1306 | 1312 | alias = join_list[-1] |
1307 | 1313 | |
1308 | 1314 | while final > 1: |
… |
… |
class BaseQuery(object):
|
1508 | 1514 | if direct: |
1509 | 1515 | if m2m: |
1510 | 1516 | # Many-to-many field defined on the current model. |
1511 | | if cached_data: |
| 1517 | skip = False |
| 1518 | if pos+1 == len(names): |
| 1519 | skip = True |
| 1520 | table = field.m2m_db_table() |
| 1521 | from_col = opts.pk.column |
| 1522 | to_col = field.m2m_column_name() |
| 1523 | target = field.m2m_reverse_name() |
| 1524 | int_alias = self.join((alias, table, from_col, to_col), dupe_multis, exclusions, nullable=True, reuse=can_reuse) |
| 1525 | joins.append(int_alias) |
| 1526 | alias = int_alias |
| 1527 | elif cached_data: |
1512 | 1528 | (table1, from_col1, to_col1, table2, from_col2, |
1513 | 1529 | to_col2, opts, target) = cached_data |
1514 | 1530 | else: |
… |
… |
class BaseQuery(object):
|
1523 | 1539 | orig_opts._join_cache[name] = (table1, from_col1, |
1524 | 1540 | to_col1, table2, from_col2, to_col2, opts, |
1525 | 1541 | target) |
1526 | | |
1527 | | int_alias = self.join((alias, table1, from_col1, to_col1), |
1528 | | dupe_multis, exclusions, nullable=True, |
1529 | | reuse=can_reuse) |
1530 | | if int_alias == table2 and from_col2 == to_col2: |
1531 | | joins.append(int_alias) |
1532 | | alias = int_alias |
1533 | | else: |
1534 | | alias = self.join( |
1535 | | (int_alias, table2, from_col2, to_col2), |
| 1542 | |
| 1543 | if not skip: |
| 1544 | int_alias = self.join((alias, table1, from_col1, to_col1), |
1536 | 1545 | dupe_multis, exclusions, nullable=True, |
1537 | 1546 | reuse=can_reuse) |
1538 | | joins.extend([int_alias, alias]) |
| 1547 | if int_alias == table2 and from_col2 == to_col2: |
| 1548 | joins.append(int_alias) |
| 1549 | alias = int_alias |
| 1550 | else: |
| 1551 | alias = self.join( |
| 1552 | (int_alias, table2, from_col2, to_col2), |
| 1553 | dupe_multis, exclusions, nullable=True, |
| 1554 | reuse=can_reuse) |
| 1555 | joins.extend([int_alias, alias]) |
1539 | 1556 | elif field.rel: |
1540 | 1557 | # One-to-one or many-to-one field |
1541 | 1558 | if cached_data: |