Opened 9 years ago

Last modified 9 years ago

#26115 closed Bug

Admin list sort using callable, latest, and admin_order_field causes duplicates. — at Initial Version

Reported by: Kevin Mann Owned by: nobody
Component: contrib.admin Version: 1.9
Severity: Normal Keywords: admin, sort, admin_order_field, list_display callable
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In a data model that records changes over time to the way two entities relate, I am able to correctly display the latest record in an admin list by creating a callable using latest(). However, when sorting the column, if there is more than one record related to the admin list parent record, the number of rows will increase to that number, showing duplicates.

An example is called for.

Given: A class Employee and a class Title, with a class EmployeeTitle that joins the two with start_date and end_date fields.

I'd like to show the employee's current title on the admin page, and be able to sort by that column. I create a callable current_title() in the Employee class which returns the latest of the employeetitle_set based on start_date.

I set the admin order field to be 'employeetitle.title.name' (double underscores don't seem to work in this editor; I've substituted ".").

If the employee has been more than one title the unsorted list displays correctly, but the list sorted by current_title shows duplicates where all the titles show the same current title.

The expected behavior is that there is a single list entry for each employee and the list is sorted by the current_title.

I'm attaching a simple model.py and admin.py for reference.

Change History (2)

by Kevin Mann, 9 years ago

Attachment: models.py added

Three models as described in bug report.

by Kevin Mann, 9 years ago

Attachment: admin.py added

Admin site configuration that yields bug.

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