Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django queryset __contains case sensitive?

I want to perform a simple query:

Pizza.object.filter(topping__contains='PEPERONI')

Like this, it works like a charm. But if I try that:

Pizza.object.filter(topping__contains='peperoni')

It is not working.

Do you know why it is case sensitive ? Is there an option to cancel this feature from django ?

like image 284
Alex Grs Avatar asked Aug 26 '12 17:08

Alex Grs


2 Answers

Use Pizza.object.filter(topping__icontains='peperoni').

Filter with __icontains check.

like image 53
Rohan Avatar answered Sep 22 '22 11:09

Rohan


You would need to import and use Q object:

from django.db.models import Q 

Resulting_Queryset = MyModel.objects.filter(Q(name__istartswith='Nishank Gupta'.strip().lower()) &  Q(name__iendswith='Nishank Gupta'.strip().lower()))

This would match Nishank Gupta and Nishank GUPTA and NisHANnk Gupta and so on. Hope this helps. Please do let me know your views.

Or Use iexact:

MyModel.objects.filter(name__iexact='Nishank Gupta'.strip().lower()) 
like image 45
Nishank Gupta Avatar answered Sep 23 '22 11:09

Nishank Gupta