'Returns a quoted list of fields to use in DISTINCT ON part of the query.
Note that 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.model._meta
    for name in self.query.distinct_fields:
        parts = name.split(LOOKUP_SEP)
        (field, col, alias, _, _) = self._setup_joins(parts, opts, None)
        (col, alias) = self._final_join_removal(col, alias)
        result.append(('%s.%s' % (qn(alias), qn2(col))))
    return result
