#30981: Admin changelist crashes when using F() and OrderBy() expressions in the
ModelAdmin's admin_order_field.
-------------------------------+------------------------------------
     Reporter:  Petr DlouhĂ˝    |                    Owner:  nobody
         Type:  Bug            |                   Status:  new
    Component:  contrib.admin  |                  Version:  master
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+------------------------------------
Changes (by felixxm):

 * version:  2.2 => master
 * type:  Uncategorized => Bug
 * stage:  Unreviewed => Accepted


Comment:

 Thanks for this report, we have here two issues. First, Admin's changelist
 crashes when using a `F()` expression in `admin_order_field`, we can fix
 this easily but checking `resolve_expression` instead of `as_sql`, e.g.
 {{{
 diff --git a/django/contrib/admin/views/main.py
 b/django/contrib/admin/views/main.py
 index 2f0e915d5e..f8b3015fb8 100644
 --- a/django/contrib/admin/views/main.py
 +++ b/django/contrib/admin/views/main.py
 @@ -308,7 +308,7 @@ class ChangeList:
                      order_field = self.get_ordering_field(field_name)
                      if not order_field:
                          continue  # No 'admin_order_field', skip it
 -                    if hasattr(order_field, 'as_sql'):
 +                    if hasattr(order_field, 'resolve_expression'):
                          # 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
 }}}
 Second issue with `F('sum_amount').desc(nulls_last=True)` is related with
 the fact that this is an instance of an `OrderBy()` expression, so
 `.asc()` and `.desc()` don't return new instances of an `OrderBy()`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30981#comment:2>
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.1cf6e5bf188d4c47e081d7d1f372c1e7%40djangoproject.com.

Reply via email to