Opened 14 years ago

Closed 14 years ago

Last modified 12 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: 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)

in reply to:  description ; comment:1 by Ramiro Morales, 14 years ago

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 by Karen Tracey, 14 years ago

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.

in reply to:  1 comment:3 by Karen Tracey, 14 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:

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

comment:4 by Kegan Gan, 14 years ago

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

NOTE: I am using trunk [11965].

comment:5 by Brian Rosner, 14 years ago

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 by Kegan Gan, 14 years ago

Thanks Brian. Looking forward to the fix.

comment:7 by ramusus, 14 years ago

Cc: ramusus@… added

comment:8 by Brian Rosner, 14 years ago

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 by Jacob, 12 years ago

milestone: 1.2

Milestone 1.2 deleted

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