#12455 closed (fixed)
admin_order_field for Admin list_display not working for callable
Reported by: | Kegan Gan | Owned by: | Brian Rosner |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Keywords: | list_display admin_order_field | |
Cc: | ramusus@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In the Admin, when using list_display, we should be able to make a sort callable if it is associated with certain database field through the use of attribute admin_order_field. Example:
def submitted_date(self, obj): return obj.created_at.strftime('%d %b %Y') submitted_date.short_description = 'Submitted Date' submitted_date.admin_order_field = 'created_at'
NOTE: created_at is a DB field in the model.
This is currently not working.
Searching through the code, I believe the bug is located in the following Django code. The attr variable seems will always be None.
Change History (9)
follow-up: 3 comment:1 by , 15 years ago
comment:2 by , 15 years ago
milestone: | → 1.2 |
---|---|
Triage Stage: | Unreviewed → Accepted |
This was broken by r11965 (added support for readonly fields), which removed a big ugly block of code that had the effect of setting attr for non-field columns in change list. It appears that the actual sorting still works on trunk, just the column header is not set properly -- if you manually construct the url to specify sorting, the results come back properly sorted. But the column header line no longer contains the link with the appropriate query string to sort on that column. There is a test for sorting on a callable in admin_views, but it apparently just checks that things sort properly when a query string that specifies sorting is received, it does not verify that the column header contains the correct link to specify sorting.
comment:3 by , 15 years ago
Replying to ramiro:
Replying to kegan:
In the Admin, when using list_display, we should be able to make a sort callable
What kind of callable is
submitted_date()
?. A a standalone function? ModelAdmin method? a model method?.
I don't believe it matters which of these the callable is. The one I tried happened to be a ModelAdmin method, but the whole block that used to set attr for all these different cases is now gone. It's still there (and working) on the 1.1.X branch:
comment:4 by , 15 years ago
Replying to ramiro: submitted_date()
is a ModelAdmin method.
NOTE: I am using trunk [11965].
comment:5 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Hmm, this looks to be an oversight on the final review before committing. I think I see where things broke down. I'll re-review tonight and get a fix in.
comment:7 by , 15 years ago
Cc: | added |
---|
comment:8 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Replying to kegan:
What kind of callable is
submitted_date()
?. A a standalone function? ModelAdmin method? a model method?.