Ticket #14977: ticket14977.diff

File ticket14977.diff, 5.0 KB (added by Evan Carmi, 14 years ago)
  • django/contrib/admin/templatetags/admin_list.py

    diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
    index 220b60e..d2338eb 100644
    a b def result_headers(cl):  
    110110
    111111        th_classes = []
    112112        new_order_type = 'asc'
    113         if field_name == cl.order_field or admin_order_field == cl.order_field:
    114             th_classes.append('sorted %sending' % cl.order_type.lower())
    115             new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()]
     113        if field_name == cl.order_field[0] or admin_order_field == cl.order_field[0]:
     114            th_classes.append('sorted %sending' % cl.order_type[0].lower())
     115            new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type[0].lower()]
    116116
    117117        yield {
    118118            "text": header,
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index 0b98f11..d34bd86 100644
    a b class ChangeList(object):  
    129129
    130130    def get_ordering(self):
    131131        lookup_opts, params = self.lookup_opts, self.params
    132         # For ordering, first check the "ordering" parameter in the admin
    133         # options, then check the object's default ordering. If neither of
    134         # those exist, order descending by ID by default. Finally, look for
    135         # manually-specified ordering from the query string.
    136         ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
    137 
    138         if ordering[0].startswith('-'):
    139             order_field, order_type = ordering[0][1:], 'desc'
    140         else:
    141             order_field, order_type = ordering[0], 'asc'
    142         if ORDER_VAR in params:
     132        # For ordering, first check manually-specified ordering from the query
     133        # string and let this override all other orderings. If there is no
     134        # manually-specified ordering check the "ordering" parameter in the
     135        # admin options, then check the object's default ordering. If neither
     136        # of those exist, order descending by ID by default.
     137
     138        # Support multiple orderings by having order_field and order_type be
     139        # lists such that order_field[i] and order_type[i] are corresponding
     140        # order_field and order_type values.
     141
     142        order_field = []
     143        order_type = []
     144        # Check for manually-specified ordering from query string.
     145        if ORDER_VAR in params and ORDER_TYPE_VAR in params:
    143146            try:
     147
     148                # Get order_type. If order_type is not 'asc' or 'desc' then
     149                # raise ValueError to 'get out' of try block and use default
     150                # ordering.
     151                if params[ORDER_TYPE_VAR] in ('asc', 'desc'):
     152                    order_type.append(params[ORDER_TYPE_VAR])
     153                else:
     154                    raise ValueError
     155
     156                # Get order_field
    144157                field_name = self.list_display[int(params[ORDER_VAR])]
    145158                try:
    146159                    f = lookup_opts.get_field(field_name)
    class ChangeList(object):  
    154167                            attr = getattr(self.model_admin, field_name)
    155168                        else:
    156169                            attr = getattr(self.model, field_name)
    157                         order_field = attr.admin_order_field
     170                        order_field.append(attr.admin_order_field)
    158171                    except AttributeError:
    159172                        pass
    160173                else:
    161                     order_field = f.name
     174                    order_field.append(f.name)
     175
    162176            except (IndexError, ValueError):
    163177                pass # Invalid ordering specified. Just use the default.
    164         if ORDER_TYPE_VAR in params and params[ORDER_TYPE_VAR] in ('asc', 'desc'):
    165             order_type = params[ORDER_TYPE_VAR]
     178
     179        if not order_field or not order_type:
     180            # Get ordering from "ordering" parameter in the admin options,
     181            # object's default ordering, or by ID descending.
     182            ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
     183            for i in range(len(ordering)):
     184                if ordering[i].startswith('-'):
     185                    order_field.append(ordering[i][1:])
     186                    order_type.append('desc')
     187                else:
     188                    order_field.append(ordering[i])
     189                    order_type.append('asc')
     190
    166191        return order_field, order_type
    167192
    168193    def get_query_set(self):
    class ChangeList(object):  
    224249
    225250        # Set ordering.
    226251        if self.order_field:
    227             qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field))
     252            ordering = ['%s%s' % ((self.order_type[i] == 'desc' and '-' or ''),
     253                self.order_field[i]) for i in range(len(self.order_field))]
     254            qs = qs.order_by(*ordering)
    228255
    229256        # Apply keyword searches.
    230257        def construct_search(field_name):
Back to Top