views:

66

answers:

1

Hello all,

I know, I can run a case insensitive search from DJango ORM. Like,

User.objects.filter(first_name__contains="jake")
User.objects.filter(first_name__contains="sulley")
User.objects.filter(first_name__icontains="Jake")
User.objects.filter(first_name__icontains="Sulley")

And also, I can fetch them as

user_list = User.objects.all().order_by("first_name")
# sequence: (Jake, Sulley, jake, sulley)
user_list = User.objects.all().order_by("-first_name") # for reverse
# sequence: (sulley, jake, Sulley, Jake)

Is there a direct way for a case-insensitive fetch?? As in I want a sequence as

# desired sequence: jake, Jake, sulley, Sulley

If not, then suggest a best way to do it. Thanks in advance.

A: 

This is for postgresql, but maybe it will be useful for other databases too:

http://scottbarnham.com/blog/2007/11/20/case-insensitive-ordering-with-django-and-postgresql/

gruszczy
But this will fail with other DB's. Also it would become complex if i need to order on fields from joined tables, right ??
simplyharsh
Have to accept a most helpful one. Thanks.
simplyharsh
You don't have to accept anything, if the answer doesn't satisfy you. Unfortunately, django doesn't provide mechanism for what you want right now. You can try to work with `extra` and this way try achieve what you want. This should work on any db. Or you can write a raw sql.
gruszczy