Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use "contains" and "iexact" at the same query in DJANGO

How can I use contains and iexact Field lookups at the same query in Django?

Like that ..

casas = Casa.objects.filter(nome_fantasia__contains__iexact='green') 
like image 308
rayashi Avatar asked Apr 05 '12 13:04

rayashi


People also ask

Can we use multiple filters in Django?

Retrieving objects. To retrieve objects from your database, construct a QuerySet via a Manager on your model class. A QuerySet represents a collection of objects from your database. It can have zero, one or many filters.

What is the difference between contains and Icontains in Django?

Definition and Usage The contains lookup is used to get records that contains a specified value. The contains lookup is case sensitive. For a case insensitive search, use the icontains lookup.

How do I do a not equal in Django QuerySet filtering?

To do a not equal in Python Django queryset filtering, we can negate a equal with ~ . to call filter with the Q object negated with ~ to return all the Entry results that don't have id 3.


1 Answers

If you need case-insensitive contains, use icontains:

casas = Casa.objects.filter(nome_fantasia__icontains = 'green') 

Which is converted to

... WHERE nome_fantasia ILIKE '%green%' 

in SQL.

like image 90
agf Avatar answered Sep 23 '22 03:09

agf