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

def _setup_aggregate_query(self, aggregates):

    opts = self.model._meta
    if (self.query.group_by is None):
        field_names = [f.attname for f in opts.concrete_fields]
        self.query.add_fields(field_names, False)
        self.query.set_group_by()
