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')
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.
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')
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')
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.
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')