#29428 closed Bug (fixed)
Add support to admin for expressions like (Lower('myfield'),) in model Meta.ordering
Reported by: | Dutch Stiphout | Owned by: | |
---|---|---|---|
Component: | contrib.admin | Version: | 2.0 |
Severity: | Release blocker | Keywords: | admin ordering expression AttributeError |
Cc: | Mariusz Felisiak, Carlton Gibson | 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
Similar to ticket #26257.
This DOES WORK in the views I've written, but when I try to access /admin/balfor/scheduledtransaction/ I get the same error as in the referenced ticket:
(Note that my app name is balfor, running Django version 2.0.4 final)
AttributeError at /admin/balfor/scheduledtransaction/ 'Lower' object has no attribute 'startswith'
models.py:
from django.db import models from django.db.models.functions import Lower class ScheduledTransaction(models.Model): description = models.CharField(max_length=50, default="New Scheduled Transaction") period = models.CharField(max_length=2, default="1S") class Meta: ordering = ['period', Lower('description')]
admin.py:
admin.site.register(ScheduledTransaction)
Change History (14)
comment:1 by , 6 years ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 6 years ago
At first look, it seems the issue was previously discovered (#28958), but the fix is incomplete (only solves for F() expressions, while the doc states "query expressions are supported".
comment:4 by , 6 years ago
Cc: | added |
---|
comment:5 by , 6 years ago
Cc: | added |
---|
A couple of points:
First, I wondered why the existing test from the fix for #28958 didn't fail since it already uses Upper
(here):
Upper(F('name')).asc(),
The key point is the asc()
call, which returns an OrderBy
(which then gets processed by the block added in the fix).
Remove the asc()
call and the test fails with the error raised in get_ordering_field_columns()
.
The ordering works without the asc()
call (or the F
for that matter) so maybe get_ordering_field_columns()
should call field.asc()
(if available, when not already an OrderBy
) in order to correct the behaviour here.
Second, whilst you can set an ordering
including (say) Lower('description')
on the model, if you try to set it on the ModelAdmin, the admin's `_check_ordering` check will error (in LOOKUP_SEP in field_name
). This occurs in for the transform or OrderBy
case. This check needs adjusting.
@Nicolas Noé Are you good to work on this ticket still?
comment:6 by , 6 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
Sorry guys, I"ve been much more busy than expected lately. Deassigning the ticket now.
comment:8 by , 6 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I was able to reproduce.