#31975: admin_order_field in ModelAdmin doesn't support sorting on multiple 
columns
-------------------------------------+-------------------------------------
     Reporter:  Petr DlouhĂ˝          |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  contrib.admin        |                  Version:  3.1
     Severity:  Normal               |               Resolution:
     Keywords:  admin_order_field,   |             Triage Stage:
  ModelAdmin                         |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Petr DlouhĂ˝):

 * status:  closed => new
 * resolution:  invalid =>


Comment:

 I tried to modify the code of the `get_ordering` function, and I came with
 following changed code (based on Django 3.0 code), that seems to be
 working as expected (it is just the section in the `try` - `catch` block):


 {{{
                     none, pfx, idx = p.rpartition('-')
                     field_name = self.list_display[int(idx)]
                     ordering_fields = self.get_ordering_field(field_name)
                     if not isinstance(ordering_fields, list):
                         ordering_fields = [ordering_fields]
                     for order_field in ordering_fields:
                         if not order_field:
                             continue  # No 'admin_order_field', skip it
                         if hasattr(order_field, 'as_sql'):
                             # order_field is an expression.
                             ordering.append(order_field.desc() if pfx ==
 '-' else order_field.asc())
                         # reverse order if order_field has already "-" as
 prefix
                         elif order_field.startswith('-') and pfx == '-':
                             ordering.append(order_field[1:])
                         else:
                             ordering.append(pfx + order_field)

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31975#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.249fc9535a86b6fce711b8263c5b0a2b%40djangoproject.com.

Reply via email to