diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index eaf2cd2..be878a4 100644
a
|
b
|
class SQLCompiler(object):
|
449 | 449 | result.append('%s%s%s' % (connector, qn(name), alias_str)) |
450 | 450 | first = False |
451 | 451 | for t in self.query.extra_tables: |
| 452 | if isinstance(t, tuple): |
| 453 | t, subselect = t |
| 454 | else: |
| 455 | subselect = None |
452 | 456 | alias, unused = self.query.table_alias(t) |
453 | 457 | # Only add the alias if it's not already present (the table_alias() |
454 | 458 | # calls increments the refcount, so an alias refcount of one means |
455 | 459 | # this is the only reference. |
456 | 460 | if alias not in self.query.alias_map or self.query.alias_refcount[alias] == 1: |
457 | 461 | connector = not first and ', ' or '' |
458 | | result.append('%s%s' % (connector, qn(alias))) |
| 462 | if subselect is None: |
| 463 | result.append('%s%s' % (connector, qn(alias))) |
| 464 | else: |
| 465 | result.append('%s%s as %s' % (connector, subselect, qn(alias))) |
459 | 466 | first = False |
460 | 467 | return result, [] |
461 | 468 | |
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 0913399..0234f4b 100644
a
|
b
|
class Query(object):
|
1637 | 1637 | if where or params: |
1638 | 1638 | self.where.add(ExtraWhere(where, params), AND) |
1639 | 1639 | if tables: |
1640 | | self.extra_tables += tuple(tables) |
| 1640 | # allow tables to be dictionaries mapping names to subselects |
| 1641 | if hasattr(tables, 'items'): |
| 1642 | self.extra_tables += tuple(tables.items()) |
| 1643 | else: |
| 1644 | self.extra_tables += tuple(tables) |
1641 | 1645 | if order_by: |
1642 | 1646 | self.extra_order_by = order_by |
1643 | 1647 | |