Opened 17 years ago

Closed 13 years ago

#4926 closed Bug (duplicate)

Ordering in admin listview ignores ordering in admin options

Reported by: Glin <glin@…> Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords: nfa-someday nfa-changelist
Cc: smoonen@…, james.m.pearson+django@…, django@…, rob@…, Patrick Kranzlmueller Triage Stage: Someday/Maybe
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When I specify ordering in admin options like this:

class ArticleOptions(admin.ModelAdmin):
    list_display = ('title', 'is_news', 'published_from_date')
    ordering = ('is_news', 'pub_date')

Ordering set default ordering column to 'is_news', but doesn't take 'pub_date' into account.

Generally all other fields except first specified in ordering tuple are ignored.

Attachments (4) (517 bytes ) - added by Glin <glin@…> 17 years ago.
ticket_4926_changelist_multifield_ordering.diff (705 bytes ) - added by Eric B <ebartels@…> 16 years ago.
ticket_4926_changelist_multifield_ordering_r12379.diff (705 bytes ) - added by Antti Kaihola 14 years ago.
patch against current trunk, otherwise unmodified
ticket_4926_changelist_multifield_ordering_r12379.2.diff (1.3 KB ) - added by Antti Kaihola 14 years ago.
also removes the related admonition from contrib.admin documentation

Download all attachments as: .zip

Change History (28)

comment:1 by Glin <glin@…>, 17 years ago

It is because query.order_by rewrites previous ordering. In this case, it is on this line:

qs = qs.order_by(order_type + lookup_order_field)

So ordering from 'ordering' admin options are not taken in account (and 'ordering' from Meta, too).

I created a patch for this.

by Glin <glin@…>, 17 years ago

Attachment: added

comment:2 by Glin <glin@…>, 17 years ago

Has patch: set

comment:3 by jkocherhans, 17 years ago

(In [6232]) newforms-admin: Fixed ordering for ModelAdmin.queryset. Refs #4926, but that ticket still needs ModelAdmin.changelist_view to use ModelAdmin.queryset.

comment:4 by jkocherhans, 17 years ago

Resolution: fixed
Status: newclosed

(In [6239]) newforms-admin: Fixed a problem with [6232] where an exception was raised if ModelAdmin didn't specify ordering. Also, fixed #4926.

comment:5 by Glin <glin@…>, 17 years ago

Resolution: fixed
Status: closedreopened

Unfortunately this did not solve the problem.

Problem is that any order_by() call deletes ordering of previous order_by() call (e.g. qs.order_by('is_news').order_by('pub_date') is the same as qs.order_by('pub_date') ).

So problem must be fixed in code of ChangeList class, because there is the last call of order_by method. (as in my patch)

comment:6 by Ramiro Morales, 17 years ago

See ticket #5673 and this discussion for a discussion about multi-column ordering in the admin UI (it's not about the admin app ordering option bout about the model's Meta.ordering setting though).

comment:7 by Glin <glin@…>, 17 years ago

I looked at it, initial comment is nearly about the same issue as this ticket (later in discussion, there is mentioned multi column sorting in way that user can influence these sorting columns.)

My patch only solves first issue, so admin still sorts primary according to one column selected by user, and other sorting columns are immutable (they are specified in model_admin.ordering). So it is only small (one and half line) patch, so way don't use this?

comment:8 by Brian Rosner, 17 years ago

Keywords: nfa-someday added

This ticket isn't critical to the merge of newforms-admin since it is an enhancement. Tagging with nfa-someday.

comment:9 by anonymous, 16 years ago

Triage Stage: UnreviewedSomeday/Maybe

comment:10 by Ales Zoulek, 16 years ago

Keywords: nfa-changelist added

comment:11 by Eric B <ebartels@…>, 16 years ago

I realized this is considered not to be critical for merging, but I'm submitting a new patch against the latest code.

Here's the new behavior: In the case where multiple fields are specified, that ordering will be respected by default. In this case, the changelist won't show a single table column as being sorted until a column header has been clicked.

This seems much more consistent to me than the current behavior.
It's a little confusing that ModelAdmin requires ordering as a tuple/list of fields, and then ignores all but the first field.

by Eric B <ebartels@…>, 16 years ago

comment:12 by smoonen@…, 16 years ago

Cc: smoonen@… added
Version: newforms-adminSVN

Me-too (now that nfa has hit the trunk). Most of my models have multiple fields in their ordering. Changing the version of this ticket to 'SVN' now that nfa is on trunk.

comment:13 by xiongchiamiov, 16 years ago

Cc: james.m.pearson+django@… added

comment:14 by Jonas von Poser, 16 years ago

Cc: django@… added

comment:15 by Xiong Chiamiov, 15 years ago

This seems like an easy fix without many consequences to me. It would be nice to either get this merged in (or a "this should go in, but the patch needs work") or a wontfix close. If the previous is the case, I'm willing to work on it. If the latter, then it'll just be nice to have a resolution of some sort.

comment:16 by Fabio Corneti, 15 years ago

This is a very useful feature (especially for django-mptt users) and the patch seems quite unobtrusive, +1 for merging it into trunk

comment:17 by Ben Davis, 15 years ago

If anyone is interested, I've submitted a patch for a proposed UI for sorting by multiple fields. This would also fix this particular issue.

See here: #11868

Any feedback is appreaciated.

by Antti Kaihola, 14 years ago

patch against current trunk, otherwise unmodified

by Antti Kaihola, 14 years ago

also removes the related admonition from contrib.admin documentation

in reply to:  8 comment:18 by Antti Kaihola, 14 years ago

Replying to brosner:

This ticket isn't critical to the merge of newforms-admin since it is an enhancement. Tagging with nfa-someday.

I'd consider this a little more than an enhancement. As Eric B noted, it's confusing that a tuple/list of fields is required but all but the first field are ignored.

comment:19 by Mogga <rob@…>, 14 years ago

I agree.

This for me, is most certainly a bug...

For example, I have a set of teams and each team has a set of players with numbers.
I would like to order them by team and then by number but the following does not do that.


In fact any number of fields are irrelevant given it really only ever orders like this:


The behavior is just wrong so for me it's a bug, not an enhancement.

comment:20 by Mogga <rob@…>, 14 years ago

Cc: rob@… added

comment:21 by Mogga <rob@…>, 14 years ago

that patch works perfectly here...

comment:22 by Patrick Kranzlmueller, 13 years ago

Cc: Patrick Kranzlmueller added

comment:23 by Gabriel Hurley, 13 years ago

Severity: Normal
Type: Bug

comment:24 by Julien Phalip, 13 years ago

Resolution: duplicate
Status: reopenedclosed

Closing as a duplicate of #11868 which, although newer, suggests a better approach UI-wise and has a more recent patch.

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