def as_sql(self, qn=None):
        if qn is None:
            qn = self.quote_name_unless_alias

        sql, params = [], []
        for aggregate in self.query.aggregate_select.values():
            agg_sql, agg_params = aggregate.as_sql(qn, self.connection)
            sql.append(agg_sql)
            params.extend(agg_params)
        sql = ', '.join(sql)
        params = tuple(params)

        sql = 'SELECT %s FROM (%s) subquery' % (sql, self.query.subquery)
        params = params + self.query.sub_params
        return sql, params
Connect to a Django database through the ORM and retrieve model fields
dataset/ETHPy150Open cloudera/hue/desktop/core/ext-py/Django-1.6.10/django/db/models/sql/compiler.py/SQLAggregateCompiler.as_sql--crossover with--dataset/ETHPy150Open saltstack/salt/salt/pillar/django_orm.py/ext_pillar