Opened 2 years ago
Closed 2 years ago
#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)
Change History (2)
by , 2 years ago
| Attachment: | django_ordering.py.diff added | 
|---|
comment:1 by , 2 years ago
| Has patch: | unset | 
|---|---|
| Resolution: | → duplicate | 
| Status: | new → closed | 
Duplicate of #31975.
patch file