2

Is it possible to sort Django queryset case insensitively? This query:

MyModel.objects.order_by('title')

uses case sensitive sorting. The result is sorted like this:

X..
a..
b..

But I would like to sort titles like this:

a..
b..
X..
niekas
  • 8,187
  • 7
  • 40
  • 58

1 Answers1

9

Not sure if you can do it with the ORM directly. But using the extra() clause, you can try doing this:

MyModel.objects.extra(select={'case_insensitive_title': 'lower(title)'}).order_by('case_insensitive_title')
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Check answer on [django-orm case-insensitive order by](https://stackoverflow.com/a/34506461/1538221). Since version 1.8 you can use `Lower` function on `order_by()` method. – slamora Jun 16 '23 at 09:09