ordering a model on multiple fields?

515 views
Skip to first unread message

hjebbers

unread,
Jul 14, 2010, 7:40:38 PM7/14/10
to Django users
is there a way to have a model class sorted on multiple fields?
in the meta class of my models I set the ordering, but django uses
only the first field for ordering;
a second field is just ignored.

(I am aware that if a users starts to sort using table headers in the
admin interface only one field will be used. It would just be great to
have the initial ordering on 2 fields.)

kind regards, henk-jan ebbers

Scott Gould

unread,
Jul 15, 2010, 9:15:57 AM7/15/10
to Django users
The Django *admin* only uses one field, ever.

Bit of an irritating limitation, I grant you, but with the use of date
hierarchies and list filters it's not too bad.

Ben Atkin

unread,
Jul 15, 2010, 11:07:50 PM7/15/10
to django...@googlegroups.com
I have a solution for you:


I'm going to post a blog entry to http://python-web.blogspot.com/ explaining my code, but it might take a little while. In the meantime, let me know if you need any help understanding the code, or run into any issues.

Ben

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Ben Atkin

unread,
Jul 16, 2010, 12:59:21 AM7/16/10
to django...@googlegroups.com
I spoke too soon! While I was writing the blog entry, I realized that due to some fixture mistake, I hadn't shuffled the data that I was sorting, so it was sorted properly without my help! I fixed the glaring mistake, and then realized that column sorting isn't applied by django.contrib.admin.ModelAdmin, but by django.contrib.admin.views.main.ChangeList. I subclassed ChangeList, and made the ModelAdmin use the subclassed ChangeList by overriding the get_changelist() method, and I'm pretty sure it's working now.

Reply all
Reply to author
Forward
0 new messages