Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

newforms-admin: Fixed #1576 -- Fixed SQL generation when using descen…

…ding

ordering from related models. Based on a patch from SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@5122 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e8b2a8649fe73b8076574efa43e4e31a45d40406 1 parent 9963ce7
@malcolmt malcolmt authored
Showing with 8 additions and 1 deletion.
  1. +8 −1 django/contrib/admin/views/main.py
View
9 django/contrib/admin/views/main.py
@@ -395,6 +395,7 @@ def get_query_set(self):
# If the order-by field is a field with a relationship, order by the
# value in the related table.
lookup_order_field = self.order_field
+ order_type = self.order_type == 'desc' and '-' or ''
try:
f = self.lookup_opts.get_field(self.order_field, many_to_many=False)
except models.FieldDoesNotExist:
@@ -405,10 +406,16 @@ def get_query_set(self):
pass
elif isinstance(f.rel, models.ManyToOneRel):
rel_ordering = f.rel.to._meta.ordering and f.rel.to._meta.ordering[0] or f.rel.to._meta.pk.column
+ if rel_ordering[0] == '-':
+ rel_ordering = rel_ordering[1:]
+ order_type = not order_type and '-' or ''
lookup_order_field = '%s.%s' % (f.rel.to._meta.db_table, rel_ordering)
+ # FIXME: Must use select_related() becuase the lookup field may
+ # be in a table not otherwise referenced yet.
+ qs = qs.select_related()
# Set ordering.
- qs = qs.order_by((self.order_type == 'desc' and '-' or '') + lookup_order_field)
+ qs = qs.order_by(order_type + lookup_order_field)
# Apply keyword searches.
def construct_search(field_name):
Please sign in to comment.
Something went wrong with that request. Please try again.