Ticket #1576: admin_fk_ordering.patch

File admin_fk_ordering.patch, 1.5 KB (added by Chris Beaven, 18 years ago)
  • django/contrib/admin/views/main.py

     
    707707        # If the order-by field is a field with a relationship, order by the
    708708        # value in the related table.
    709709        lookup_order_field = self.order_field
     710        order_type = (self.order_type == 'desc' and '-' or '')
    710711        try:
    711712            f = self.lookup_opts.get_field(self.order_field, many_to_many=False)
    712713        except models.FieldDoesNotExist:
     
    717718                pass
    718719            elif isinstance(f.rel, models.ManyToOneRel):
    719720                rel_ordering = f.rel.to._meta.ordering and f.rel.to._meta.ordering[0] or f.rel.to._meta.pk.column
     721                if rel_ordering.startswith('-'):
     722                    rel_ordering = rel_ordering[1:]
     723                    order_type = (not order_type and '-' or '')
    720724                lookup_order_field = '%s.%s' % (f.rel.to._meta.db_table, rel_ordering)
     725                # Must select related because the lookup field may be in a table not
     726                # otherwise referenced yet.
     727                qs = qs.select_related()
    721728
    722729        # Set ordering.
    723         qs = qs.order_by((self.order_type == 'desc' and '-' or '') + lookup_order_field)
     730        print lookup_order_field
     731        qs = qs.order_by('%s%s' % (order_type, lookup_order_field))
    724732
    725733        # Apply keyword searches.
    726734        def construct_search(field_name):
Back to Top