P
P
Pyramidec2017-08-26 17:43:51
Django
Pyramidec, 2017-08-26 17:43:51

How to order list items case insensitively?

You need to sort alphabetically regardless of case. Those. "B" should not be higher than "a". I know that uppercase codes come before lowercase ones, so the sorting itself needs to be done in lowercase.
I read in the documentation about the following method:

from django.db.models.functions import Lower

Article.objects.order_by(Lower('name').asc())

But the end result is the same. Perhaps he did something wrong. Help me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2017-08-26
@deliro

As an option:

Article.objects.extra(select={'name_lower': 'LOWER(name)'}).order_by('name_lower')

D
Dmitry Matveev, 2017-08-26
@Shlem

You can try through annotate, then order_by.
https://docs.djangoproject.com/en/1.11/topics/db/a...
Something like Article.objects.annotate(name_lower=Lower('name')).order_by('name_lower')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question