The Official Kountr Blog

Just another WordPress.com weblog

Django’s “order_by”

with 2 comments

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.

Written by Michael Artemiw

June 17, 2008 at 5:43 am

Posted in Uncategorized

2 Responses

Subscribe to comments with RSS.

  1. 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

  2. oops, not smile at the end of my comment 🙂
    .order_by( ’lower_field’ )

    Vitaliy

    June 18, 2008 at 9:26 am


Leave a comment