David542
David542

Reputation: 110203

Lowercase django query

Is it possible to do the following without doing raw sql or post-processing in django?

dict(Language.objects.values_list('code' LOWER(),'language'))

Upvotes: 11

Views: 11898

Answers (1)

Alasdair
Alasdair

Reputation: 308899

This can be done in Django 1.8+ by importing the Lower database function.

from django.db.models.functions import Lower
qs = Language.objects.annotate(code_lower=Lower('code')) # annotate each item in the queryset with the code_lower
values = qs.values_list('code_lower', 'language')
dictionary = dict(values)

I've split the answer into several lines for readability, you can collapse into a one liner if you prefer.

Upvotes: 30

Related Questions