          assert set(change_map.keys()).intersection(set(change_map.values())) == set()
  
          # 1. Update references in "select" and "where".
          self.where.relabel_aliases(change_map)
          for pos, col in enumerate(self.select):
              if isinstance(col, (list, tuple)):
                  old_alias = col[0]
                  self.select[pos] = (change_map.get(old_alias, old_alias), col[1])
              else:
                  col.relabel_aliases(change_map)
  
          # 2. Rename the alias in the internal table/alias datastructures.
          for old_alias, new_alias in change_map.iteritems():
              alias_data = list(self.alias_map[old_alias])
              alias_data[RHS_ALIAS] = new_alias
 
</s> remove         # 1. Update references in "select" and "where".
 </s> add         # 1. Update references in "select" (normal columns plus aliases),
        # "group by", "where" and "having". </s> remove             aggregate.lookup in self.aggregate_select.keys()):
 </s> add             aggregate.lookup in self.aggregates.keys()): </s> remove         self.aggregate_select = SortedDict() # Maps alias -> SQL aggregate function
 </s> add         self.aggregates = SortedDict() # Maps alias -> SQL aggregate function
        self.aggregate_select_mask = None
        self._aggregate_select_cache = None </s> remove         obj._setup_aggregate_query()
 </s> add         obj._setup_aggregate_query(kwargs.keys()) </s> remove         obj.aggregate_select = self.aggregate_select.copy()
 </s> add         obj.aggregates = self.aggregates.copy()
        if self.aggregate_select_mask is None:
            obj.aggregate_select_mask = None
        else:
            obj.aggregate_select_mask = self.aggregate_select_mask[:]
        if self._aggregate_select_cache is None:
            obj._aggregate_select_cache = None
        else:
            obj._aggregate_select_cache = self._aggregate_select_cache.copy()
        self.having.relabel_aliases(change_map)
        for columns in (self.select, self.aggregates.values(), self.group_by or []):
            for pos, col in enumerate(columns):
                if isinstance(col, (list, tuple)):
                    old_alias = col[0]
                    columns[pos] = (change_map.get(old_alias, old_alias), col[1])
                else:
                    col.relabel_aliases(change_map)
          assert set(change_map.keys()).intersection(set(change_map.values())) == set()
  
          # 1. Update references in "select" and "where".
          self.where.relabel_aliases(change_map)
        self.having.relabel_aliases(change_map)
        for columns in (self.select, self.aggregates.values(), self.group_by or []):
            for pos, col in enumerate(columns):
                if isinstance(col, (list, tuple)):
                    old_alias = col[0]
                    columns[pos] = (change_map.get(old_alias, old_alias), col[1])
                else:
                    col.relabel_aliases(change_map)
        self.having.relabel_aliases(change_map)
        for columns in (self.select, self.aggregates.values(), self.group_by or []):
            for pos, col in enumerate(columns):
                if isinstance(col, (list, tuple)):
                    old_alias = col[0]
                    columns[pos] = (change_map.get(old_alias, old_alias), col[1])
                else:
                    col.relabel_aliases(change_map)
        self.having.relabel_aliases(change_map)
        for columns in (self.select, self.aggregates.values(), self.group_by or []):
            for pos, col in enumerate(columns):
                if isinstance(col, (list, tuple)):
                    old_alias = col[0]
                    columns[pos] = (change_map.get(old_alias, old_alias), col[1])
                else:
                    col.relabel_aliases(change_map)
        self.having.relabel_aliases(change_map)
        for columns in (self.select, self.aggregates.values(), self.group_by or []):
            for pos, col in enumerate(columns):
                if isinstance(col, (list, tuple)):
                    old_alias = col[0]
                    columns[pos] = (change_map.get(old_alias, old_alias), col[1])
                else:
                    col.relabel_aliases(change_map)
        self.having.relabel_aliases(change_map)
        for columns in (self.select, self.aggregates.values(), self.group_by or []):
            for pos, col in enumerate(columns):
                if isinstance(col, (list, tuple)):
                    old_alias = col[0]
                    columns[pos] = (change_map.get(old_alias, old_alias), col[1])
                else:
                    col.relabel_aliases(change_map)
        self.having.relabel_aliases(change_map)
        for columns in (self.select, self.aggregates.values(), self.group_by or []):
            for pos, col in enumerate(columns):
                if isinstance(col, (list, tuple)):
                    old_alias = col[0]
                    columns[pos] = (change_map.get(old_alias, old_alias), col[1])
                else:
                    col.relabel_aliases(change_map)
  
          # 2. Rename the alias in the internal table/alias datastructures.
          for old_alias, new_alias in change_map.iteritems():
              alias_data = list(self.alias_map[old_alias])
              alias_data[RHS_ALIAS] = new_alias
keep keep keep keep replace replace replace replace replace replace keep keep keep keep keep
 Fixed #10182 -- Corrected realiasing and the process of evaluating values() for queries with aggregate clauses. This means that aggregate queries can now be used as subqueries (such as in an __in clause). Thanks to omat for the report.

This involves a slight change to the interaction of annotate() and values() clauses that specify a list of columns. See the docs for details.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9888 bcc190cf-cafb-0310-a4f2-bffc1f526a37 
django/db/models/sql/query.py