Django’s “order_by”
Django is awesome for all of the “we’ve already done it so you don’t have to” features. So I am always caught off guard when it falls short.
When you have a QuerySet you can use order_by to order the set before display. This is really handy. Unfortunately, you cannot instruct it to ignore the case of the strings. Debugging this problem was even harder because I stored the strings in the DB as they were entered by users, but only display them in all lowercase on the website. I couldn’t figure out why the list was in two parts, both sorted correctly.
As a fix, I have set the strings to lowercase before they are written to the DB, and gone and changed the current ones to all lowercase. The SQL looks something like;
update table set column=lower(column);
Overall not a huge problem, but it would be pretty slick if Django allowed you to do;
Count.objects.filter(…).order_by(‘name’, ‘ignorecase’
Hope I saved you some anguish.
I guess you have that problem for postresql…
I usualy use hack like this:
SomeModel.objects.all().extra(select={‘lower_field’: ‘lower(field)’}).order_by(‘lower_field’)
Vitaliy
June 18, 2008 at 9:25 am
oops, not smile at the end of my comment 🙂
.order_by( ’lower_field’ )
Vitaliy
June 18, 2008 at 9:26 am