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 , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 8 years ago
Summary: | ModelAdmin list_display callable does not support query expressions in admin_order_field → Add query expression support for ModelAdmin list_display's admin_order_field |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 8 years ago
Has patch: | set |
---|
comment:4 by , 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 , 7 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
PR