'Returns the list of ordering fields for the change list.
First we check the get_ordering() method in model admin, then we check
the object\'s default ordering. Then, any manually-specified ordering
from the query string overrides anything. Finally, a deterministic
order is guaranteed by ensuring the primary key is used as the last
ordering field.'

def get_ordering(self, request, queryset):

    params = self.params
    ordering = list((self.model_admin.get_ordering(request) or self._get_default_ordering()))
    if (ORDER_VAR in params):
        ordering = []
        order_params = params[ORDER_VAR].split('.')
        for p in order_params:
            try:
                (none, pfx, idx) = p.rpartition('-')
                field_name = self.list_display[int(idx)]
                order_field = self.get_ordering_field(field_name)
                if (not order_field):
                    continue
                ordering.append((pfx + order_field))
            except (IndexError, ValueError):
                continue
    ordering.extend(queryset.query.order_by)
    pk_name = self.lookup_opts.pk.name
    if (not (set(ordering) & set(['pk', '-pk', pk_name, ('-' + pk_name)]))):
        ordering.append('-pk')
    return ordering
