Opened 8 years ago

Closed 7 years ago

#27810 closed New feature (fixed)

Add query expression support for ModelAdmin list_display's admin_order_field

Reported by: Andreas Pelme Owned by: Andreas Pelme
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Query expressions cannot currently be used with admin_order_field in a callable on list_display. The code expects a string and crashes when an query expression object is passed instead. This example shows the problem:

def full_name(obj):
    return f'{obj.first_name} {obj.last_name}'
my_callable.admin_order_field = Concat(F('first_name'), F('last_name'))

... which leads to a crash like this:

...
    self.queryset = self.get_queryset(request)
  File "/Users/andreas/code/django/django/contrib/admin/views/main.py", line 346, in get_queryset
    ordering = self.get_ordering(request, qs)
  File "/Users/andreas/code/django/django/contrib/admin/views/main.py", line 260, in get_ordering
    if order_field.startswith('-') and pfx == "-":
AttributeError: 'CombinedExpression' object has no attribute 'startswith'

It would make sense to be able to make use of all the nice stuff that has gone into query expression from the admin in this case! :)

Change History (6)

comment:1 by Andreas Pelme, 8 years ago

Owner: changed from nobody to Andreas Pelme
Status: newassigned

comment:2 by Tim Graham, 8 years ago

Summary: ModelAdmin list_display callable does not support query expressions in admin_order_fieldAdd query expression support for ModelAdmin list_display's admin_order_field
Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 8 years ago

Has patch: set

comment:4 by Simon Wiles, 7 years ago

any chance of this getting committed? it would be very useful to have...

(specific use-case that drove me here: sorting in admin on a DateField that has NULLs...)

comment:5 by Carlton Gibson, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In e307ff29:

Fixed #27810 -- Allowed query expressions in admin_order_field.

Note: See TracTickets for help on using tickets.
Back to Top