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

Question:

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')
Asked By: rayashi

||

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.

Answered By: agf

Honestly, you don’t need to. The two resultsets overlap. If you were intending AND then just use the most restrictive: __iexact. if you want OR use __contains

Just to answer your question you could do something like below (note this is an AND)

casas = Casa.objects.filter(nome_fantasia__contains='green', nome_fantasia__iexact='green')
Answered By: Francis Yaconiello
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.