Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#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: master
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: UI/UX:

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)

comment:1 in reply to:  description ; Changed 7 years ago by Ramiro Morales

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

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?.

comment:2 Changed 7 years ago by Karen Tracey

milestone: 1.2
Triage Stage: UnreviewedAccepted

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 in reply to:  1 Changed 7 years ago by Karen Tracey

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:

http://code.djangoproject.com/browser/django/branches/releases/1.1.X/django/contrib/admin/templatetags/admin_list.py?rev=11965#L87

comment:4 Changed 7 years ago by Kegan Gan

Replying to ramiro: submitted_date() is a ModelAdmin method.

NOTE: I am using trunk [11965].

comment:5 Changed 7 years ago by Brian Rosner

Owner: changed from nobody to Brian Rosner
Status: newassigned

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:6 Changed 7 years ago by Kegan Gan

Thanks Brian. Looking forward to the fix.

comment:7 Changed 7 years ago by ramusus

Cc: ramusus@… added

comment:8 Changed 7 years ago by Brian Rosner

Resolution: fixed
Status: assignedclosed

(In [12157]) Fixed #12455 -- corrected an oversight in result_headers not honoring admin_order_field

This finishes some slightly refactoring that went into the admin_list
template tags.

Thanks to kegan for discovering the oversight.

comment:9 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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