'Return a quoted list of fields to use in DISTINCT ON part of the query.
This method can alter the tables in the query, and thus it must be
called before get_from_clause().'

def get_distinct(self):

    qn = self.quote_name_unless_alias
    qn2 = self.connection.ops.quote_name
    result = []
    opts = self.query.get_meta()
    for name in self.query.distinct_fields:
        parts = name.split(LOOKUP_SEP)
        (_, targets, alias, joins, path, _) = self._setup_joins(parts, opts, None)
        (targets, alias, _) = self.query.trim_joins(targets, joins, path)
        for target in targets:
            if (name in self.query.annotation_select):
                result.append(name)
            else:
                result.append(('%s.%s' % (qn(alias), qn2(target.column))))
    return result
