diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
index b450e89..08d1919 100644
a
|
b
|
class ChangeList(object):
|
131 | 131 | |
132 | 132 | def get_ordering(self): |
133 | 133 | lookup_opts, params = self.lookup_opts, self.params |
134 | | # For ordering, first check the "ordering" parameter in the admin |
135 | | # options, then check the object's default ordering. If neither of |
136 | | # those exist, order descending by ID by default. Finally, look for |
137 | | # manually-specified ordering from the query string. |
138 | | ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name] |
139 | | |
140 | | if ordering[0].startswith('-'): |
141 | | order_field, order_type = ordering[0][1:], 'desc' |
| 134 | # If the root query set has no ordering, order descending by ID. |
| 135 | # Set explicit ordering if it is specified in the query string. |
| 136 | if self.root_query_set.query.has_ordering(): |
| 137 | # self.root_queryset already has ordering, don't over-ride it |
| 138 | order_field, order_type = None, 'asc' |
142 | 139 | else: |
143 | | order_field, order_type = ordering[0], 'asc' |
| 140 | order_field, order_type = lookup_opts.pk.name, 'desc' |
| 141 | |
144 | 142 | if ORDER_VAR in params: |
145 | 143 | try: |
146 | 144 | field_name = self.list_display[int(params[ORDER_VAR])] |
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 3381b60..de0e35e 100644
a
|
b
|
class BaseQuery(object):
|
596 | 596 | result.append(elt) |
597 | 597 | return result, params |
598 | 598 | |
| 599 | def has_ordering(self): |
| 600 | """ |
| 601 | Return True if ordering will be set on this query. |
| 602 | """ |
| 603 | return bool(self.extra_order_by or self.order_by or (self.default_ordering and self.model._meta.ordering)) |
| 604 | |
599 | 605 | def get_ordering(self): |
600 | 606 | """ |
601 | 607 | Returns list representing the SQL elements in the "order by" clause. |