Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lowercase django query

Tags:

python

django

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

dict(Language.objects.values_list('code' LOWER(),'language'))
like image 887
David542 Avatar asked Jul 30 '15 22:07

David542


1 Answers

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.

like image 182
Alasdair Avatar answered Oct 23 '22 18:10

Alasdair