summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJulien Phalip <jphalip@gmail.com>2011-11-22 09:14:09 +0000
committerJulien Phalip <jphalip@gmail.com>2011-11-22 09:14:09 +0000
commit8ddecc39dbbb4da17a4eccff29bcf95efed991b8 (patch)
treed48193e251fb70012543d550a7fa10a9d711a78a /django
parent658abb0859382a07f88b2fe8e77d6c0035fcd456 (diff)
Fixed #17252 -- Fixed a minor regression introduced by the work in #11868, where the default sorted columns wouldn't correctly be visually represented in the changelist table headers if those columns referred to non model fields. Thanks to sebastian for the report and patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17143 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/contrib/admin/views/main.py69
1 files changed, 33 insertions, 36 deletions
diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
index 4eed0160e2..0294be187e 100644
--- a/django/contrib/admin/views/main.py
+++ b/django/contrib/admin/views/main.py
@@ -169,6 +169,28 @@ class ChangeList(object):
ordering = self.lookup_opts.ordering
return ordering
+ def get_ordering_field(self, field_name):
+ """
+ Returns the proper model field name corresponding to the given
+ field_name to use for ordering. field_name may either be the name of a
+ proper model field or the name of a method (on the admin or model) or a
+ callable with the 'admin_order_field' attribute. Returns None if no
+ proper model field name can be matched.
+ """
+ try:
+ field = self.lookup_opts.get_field(field_name)
+ return field.name
+ except models.FieldDoesNotExist:
+ # See whether field_name is a name of a non-field
+ # that allows sorting.
+ if callable(field_name):
+ attr = field_name
+ elif hasattr(self.model_admin, field_name):
+ attr = getattr(self.model_admin, field_name)
+ else:
+ attr = getattr(self.model, field_name)
+ return getattr(attr, 'admin_order_field', None)
+
def get_ordering(self, request):
params = self.params
# For ordering, first check the if exists the "get_ordering" method
@@ -176,7 +198,6 @@ class ChangeList(object):
# options, then check the object's default ordering. Finally, a
# manually-specified ordering from the query string overrides anything.
ordering = self.model_admin.get_ordering(request) or self._get_default_ordering()
-
if ORDER_VAR in params:
# Clear ordering and used params
ordering = []
@@ -185,33 +206,18 @@ class ChangeList(object):
try:
none, pfx, idx = p.rpartition('-')
field_name = self.list_display[int(idx)]
- try:
- f = self.lookup_opts.get_field(field_name)
- except models.FieldDoesNotExist:
- # See whether field_name is a name of a non-field
- # that allows sorting.
- try:
- if callable(field_name):
- attr = field_name
- elif hasattr(self.model_admin, field_name):
- attr = getattr(self.model_admin, field_name)
- else:
- attr = getattr(self.model, field_name)
- field_name = attr.admin_order_field
- except AttributeError:
- continue # No 'admin_order_field', skip it
- else:
- field_name = f.name
-
- ordering.append(pfx + field_name)
-
+ order_field = self.get_ordering_field(field_name)
+ if not order_field:
+ continue # No 'admin_order_field', skip it
+ ordering.append(pfx + order_field)
except (IndexError, ValueError):
continue # Invalid ordering specified, skip it.
-
return ordering
def get_ordering_field_columns(self):
- # Returns a SortedDict of ordering field column numbers and asc/desc
+ """
+ Returns a SortedDict of ordering field column numbers and asc/desc
+ """
# We must cope with more than one column having the same underlying sort
# field, so we base things on column numbers.
@@ -227,19 +233,10 @@ class ChangeList(object):
order_type = 'desc'
else:
order_type = 'asc'
- index = None
- try:
- # Search for simply field name first
- index = list(self.list_display).index(field)
- except ValueError:
- # No match, but there might be a match if we take into
- # account 'admin_order_field'
- for _index, attr in enumerate(self.list_display):
- if getattr(attr, 'admin_order_field', '') == field:
- index = _index
- break
- if index is not None:
- ordering_fields[index] = order_type
+ for index, attr in enumerate(self.list_display):
+ if self.get_ordering_field(attr) == field:
+ ordering_fields[index] = order_type
+ break
else:
for p in self.params[ORDER_VAR].split('.'):
none, pfx, idx = p.rpartition('-')