Fixed #1576 -- Fixed incorrect SQL generated when using descending ordering

from related models. Patch from SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5092 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-04-26 13:48:31 +00:00
parent 439cb4047f
commit 6c18ba8c91
1 changed files with 10 additions and 1 deletions

View File

@ -707,6 +707,7 @@ class ChangeList(object):
# 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:
@ -717,10 +718,18 @@ class ChangeList(object):
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)
# Must select related because the lookup field may be in a
# table not otherwise referenced yet.
# FIXME: Try to remove the need for this in the QuerySet
# refactor.
qs = qs.select_related()
# Set ordering.
qs = qs.order_by((self.order_type == 'desc' and '-' or '') + lookup_order_field)
qs = qs.order_by('%s%s' % (order_type, lookup_order_field))
# Apply keyword searches.
def construct_search(field_name):