'Returns a tuple containing a list representing the SQL elements in the
"order by" clause, and the list of SQL elements that need to be added
to the GROUP BY clause as a result of the ordering.
Also sets the ordering_aliases attribute on this instance to a list of
extra aliases needed in the select.
Determining the ordering SQL can change the tables we need to include,
so this should be run *before* get_from_clause().'

def get_ordering(self):

    if self.query.extra_order_by:
        ordering = self.query.extra_order_by
    elif (not self.query.default_ordering):
        ordering = self.query.order_by
    else:
        ordering = (self.query.order_by or self.query.get_meta().ordering or [])
    qn = self.quote_name_unless_alias
    qn2 = self.connection.ops.quote_name
    distinct = self.query.distinct
    select_aliases = self._select_aliases
    result = []
    group_by = []
    ordering_aliases = []
    if self.query.standard_ordering:
        (asc, desc) = ORDER_DIR['ASC']
    else:
        (asc, desc) = ORDER_DIR['DESC']
    processed_pairs = set()
    params = []
    ordering_params = []
    for (pos, field) in enumerate(ordering):
        if (field == '?'):
            result.append(self.connection.ops.random_function_sql())
            continue
        if isinstance(field, int):
            if (field < 0):
                order = desc
                field = (- field)
            else:
                order = asc
            result.append(('%s    %s' % (field, order)))
            group_by.append((str(field), []))
            continue
        (col, order) = get_order_dir(field, asc)
        if (col in self.query.aggregate_select):
            result.append(('%s    %s' % (qn(col), order)))
            continue
        if ('.' in field):
            (table, col) = col.split('.', 1)
            if ((table, col) not in processed_pairs):
                elt = ('%s.%s' % (qn(table), col))
                processed_pairs.add((table, col))
                if ((not distinct) or (elt in select_aliases)):
                    result.append(('%s    %s' % (elt, order)))
                    group_by.append((elt, []))
        elif (get_order_dir(field)[0] not in self.query.extra):
            for (table, cols, order) in self.find_ordering_name(field, self.query.get_meta(), default_order=asc):
                for col in cols:
                    if ((table, col) not in processed_pairs):
                        elt = ('%s.%s' % (qn(table), qn2(col)))
                        processed_pairs.add((table, col))
                        if (distinct and (elt not in select_aliases)):
                            ordering_aliases.append(elt)
                        result.append(('%s    %s' % (elt, order)))
                        group_by.append((elt, []))
        else:
            elt = qn2(col)
            if (col not in self.query.extra_select):
                sql = ('(%s)    AS    %s' % (self.query.extra[col][0], elt))
                ordering_aliases.append(sql)
                ordering_params.extend(self.query.extra[col][1])
            elif (distinct and (col not in select_aliases)):
                ordering_aliases.append(elt)
                ordering_params.extend(params)
            result.append(('%s    %s' % (elt, order)))
            group_by.append(self.query.extra[col])
    self.ordering_aliases = ordering_aliases
    self.ordering_params = ordering_params
    return (result, params, group_by)
