'Prepare the query for computing a result that contains aggregate annotations.'

def _setup_aggregate_query(self, aggregates):

    self.query.set_group_by()
    if (self.aggregate_names is not None):
        self.aggregate_names.extend(aggregates)
        self.query.set_aggregate_mask(self.aggregate_names)
    super(ValuesQuerySet, self)._setup_aggregate_query(aggregates)
