{{{
# models.py
from __future__ import unicode_literals
from django.db import models
class CreditCard(models.Model):
issued_to = models.CharField(max_length=40)
valid_to = 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',
'valid_to',
'last_four_digits',
]
ordering = ['valid_to']
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'
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 without having to resort to things
like
{{{
class CCAdmin(admin.ModelAdmin):
list_display = [
'issued_to',
'valid_to',
'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'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25790>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
New description:
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)
valid_to = 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',
'valid_to',
'last_four_digits',
]
ordering = ['valid_to']
admin.site.register(CreditCard, CCAdmin)
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:1>
* needs_docs: 0 => 1
* has_patch: 0 => 1
* needs_tests: 0 => 1
Comment:
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 ORed with `ordering` to get the final list of columns which
will actually be allowed to sort by.
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:2>
* stage: Unreviewed => Accepted
* version: 1.8 => master
Comment:
I guess we should add the the `get_orderable_by(request)` version too.
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:3>
* owner: nobody => sasha0
* needs_docs: 1 => 0
* status: new => assigned
* needs_tests: 1 => 0
Comment:
[https://github.com/django/django/pull/6107 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:4>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:5>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:6>
* stage: Accepted => Ready for checkin
Comment:
The PR looks good to me: the patch submitter has answered all questions,
added tests, and all checks pass.
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:7>
* needs_better_patch: 0 => 1
* needs_docs: 0 => 1
* stage: Ready for checkin => Accepted
Comment:
The documentation requires adjustments as the feature didn't make it in
1.10.
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:8>
Old description:
> 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)
> valid_to = 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',
> 'valid_to',
> 'last_four_digits',
> ]
> ordering = ['valid_to']
>
> admin.site.register(CreditCard, CCAdmin)
New description:
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'
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:9>
* owner: Sasha Gaevsky => Simon Charette
* cc: Simon Charette (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:10>
* cc: Matthijs Kooijman (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:11>
* owner: Simon Charette => Ramiro Morales
Comment:
I'm working on updating this.
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:12>
* needs_better_patch: 1 => 0
* needs_docs: 1 => 0
Comment:
[https://github.com/django/django/pull/9538 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:13>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:14>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"ef2512b2ffdb719e5c0fb82142f9ce8478282823" ef2512b2]:
{{{
#!CommitTicketReference repository=""
revision="ef2512b2ffdb719e5c0fb82142f9ce8478282823"
Fixed #25790 -- Allowed disable column sorting in the admin changelist.
Thanks Ramiro Morales for completing the patch.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25790#comment:15>