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 )
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.ordering
option.
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 , 9 years ago
Description: | modified (diff) |
---|
comment:2 by , 9 years ago
Has patch: | set |
---|---|
Needs documentation: | set |
Needs tests: | set |
comment:3 by , 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: | Unreviewed → Accepted |
Version: | 1.8 → master |
I guess we should add the the get_orderable_by(request)
version too.
comment:4 by , 9 years ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
Owner: | changed from | to
Status: | new → assigned |
comment:5 by , 9 years ago
Patch needs improvement: | set |
---|
comment:6 by , 9 years ago
Patch needs improvement: | unset |
---|
comment:7 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
The PR looks good to me: the patch submitter has answered all questions, added tests, and all checks pass.
comment:8 by , 9 years ago
Needs documentation: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Ready for checkin → Accepted |
The documentation requires adjustments as the feature didn't make it in 1.10.
comment:9 by , 8 years ago
Description: | modified (diff) |
---|
comment:10 by , 8 years ago
Cc: | added |
---|---|
Owner: | changed from | to
comment:11 by , 7 years ago
Cc: | added |
---|
comment:14 by , 7 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
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 withordering
to get the final list of columns which will actually be allowed to sort by.