Ticket #1576: admin_fk_ordering.patch
File admin_fk_ordering.patch, 1.5 KB (added by , 18 years ago) |
---|
-
django/contrib/admin/views/main.py
707 707 # If the order-by field is a field with a relationship, order by the 708 708 # value in the related table. 709 709 lookup_order_field = self.order_field 710 order_type = (self.order_type == 'desc' and '-' or '') 710 711 try: 711 712 f = self.lookup_opts.get_field(self.order_field, many_to_many=False) 712 713 except models.FieldDoesNotExist: … … 717 718 pass 718 719 elif isinstance(f.rel, models.ManyToOneRel): 719 720 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 '') 720 724 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() 721 728 722 729 # 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)) 724 732 725 733 # Apply keyword searches. 726 734 def construct_search(field_name):