Opened 9 years ago

Closed 7 years ago

#25790 closed New feature (fixed)

Add a way to disable column sorting in the admin

Reported by: Ramiro Morales Owned by: Ramiro Morales
Component: contrib.admin Version: dev
Severity: Normal Keywords: sorting ordering change list changelist admin
Cc: Simon Charette, Matthijs Kooijman Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

Consider this model:

# models.py
from __future__ import unicode_literals

from django.db import models


class CreditCard(models.Model):
    issued_to = models.CharField(max_length=40)
    good_thru = models.DateField()
    last_four_digits = models.CharField(max_length=4)

And this admin.py for the app:

from django.contrib import admin

from app.models import CreditCard


class CCAdmin(admin.ModelAdmin):
    list_display = [
        'issued_to',
        'good_thru',
        'last_four_digits',
    ]
    ordering = ['good_thru']


admin.site.register(CreditCard, CCAdmin)

The user has specified explicitly he/she wants the change list view grid to be sortable by default by the credt card expiration date by using the ModelAdmin.orderingoption.

Now, the interactive sorting changelist functionality that allows one to sort by a column by clicking on its header automagically allows one to also sort by other columns chosen by a documented logic (i.e. not if callable columns, etc.).

In the example, it allows users to also sort by the name of the credit card owner which, even if not asked for, seems useful.

Where it doesn't make so much sense is, for the example, in the case of the 'last four digits' column.

IMHO there should be a way to express which columns one wants this functionality for without having to resort to things like:

class CCAdmin(admin.ModelAdmin):
    list_display = [
        'issued_to',
        'good_thru',
        'last4digits',
    ]
    #...

    def last4digits(self, obj):
        """So we don't get bogus ordering by this field in the change list view."""
        return obj.last_four_digits
    last4digits.short_description = '4 last digits'

Change History (15)

comment:1 by Ramiro Morales, 9 years ago

Description: modified (diff)

comment:2 by Ramiro Morales, 9 years ago

Has patch: set
Needs documentation: set
Needs tests: set

There is some work in this branch: https://github.com/django/django/compare/master...ramiro:ticket_25790?expand=1

I'm thinking now it's wrong or at least incomplete.

I suspect this is overloading the ModelAdmin.ordering option (which is for specifying the change list default/initial ordering) with an additional function.

A possible solution, provided the feature proposed by this ticket is accepted, is to have another option e.g. orderable_by which when not provided makes things behave like they do now and when specified (a list of fields) is intersected with ordering to get the final list of columns which will actually be allowed to sort by.

Last edited 7 years ago by Ramiro Morales (previous) (diff)

comment:3 by Tim Graham, 9 years ago

Summary: Admin magic change list UI ordering is too helpful?Add a way to disable column sorting in the admin
Triage Stage: UnreviewedAccepted
Version: 1.8master

I guess we should add the the get_orderable_by(request) version too.

comment:4 by Sasha Gaevsky, 9 years ago

Needs documentation: unset
Needs tests: unset
Owner: changed from nobody to Sasha Gaevsky
Status: newassigned

comment:5 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:6 by Sasha Gaevsky, 9 years ago

Patch needs improvement: unset

comment:7 by Philip James, 9 years ago

Triage Stage: AcceptedReady for checkin

The PR looks good to me: the patch submitter has answered all questions, added tests, and all checks pass.

comment:8 by Simon Charette, 9 years ago

Needs documentation: set
Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

The documentation requires adjustments as the feature didn't make it in 1.10.

comment:9 by Ramiro Morales, 8 years ago

Description: modified (diff)

comment:10 by Simon Charette, 8 years ago

Cc: Simon Charette added
Owner: changed from Sasha Gaevsky to Simon Charette

comment:11 by Matthijs Kooijman, 7 years ago

Cc: Matthijs Kooijman added

comment:12 by Ramiro Morales, 7 years ago

Owner: changed from Simon Charette to Ramiro Morales

I'm working on updating this.

comment:13 by Ramiro Morales, 7 years ago

Needs documentation: unset
Patch needs improvement: unset

comment:14 by Carlton Gibson, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:15 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In ef2512b2:

Fixed #25790 -- Allowed disable column sorting in the admin changelist.

Thanks Ramiro Morales for completing the patch.

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