#34646 closed New feature (duplicate)

Ordering a Django admin column based on multiple model fields

Reported by: Willem Van Onsem Owned by: nobody
Component: contrib.admin Version: 4.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

This issue is based on [this StackOverflow question](https://stackoverflow.com/q/76425892/67579). Here a Django admin column is made:

class Case(models.Model):
    number = models.SmallIntegerField()
    year = models.SmallIntegerField()

with an admin column that combines the number and year:

@admin.register(Case)
class CaseAdmin(admin.ModelAdmin):
    list_display = ['case_number']
    ordering = ['-year', '-number']

    @admin.display(ordering=['-year', '-number'])
    def case_number(self, case):
        return f'{case.number} / {case.year}'

Currently it is not possible to use an ordering that is a tuple of "orderables". We can for example use Concat('year', Value('/'), 'number'), but that will not work for numbers, since '10' < '2' for strings.

The modification seems not to be that difficult, we can look if it is a list/tuple and then do the same logic, for each item in the list and add these to the fields.

Attachments (1)

django_ordering.py.diff (3.8 KB ) - added by Willem Van Onsem 18 months ago.
patch file

Download all attachments as: .zip

Change History (2)

by Willem Van Onsem, 18 months ago

Attachment: django_ordering.py.diff added

patch file

comment:1 by Mariusz Felisiak, 18 months ago

Has patch: unset
Resolution: duplicate
Status: newclosed

Duplicate of #31975.

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