Opposite to Django icontains

887 views Asked by At

How can I update the following query so that I need to remove u like "29" (u__inotcontains="29")?

model1_obj = xx.objects.filter(
    date_generated__gte=1429142401,
    date_generated__lte=1431648001,
    s__icontains="28"
)
2

There are 2 answers

4
Edwin Lunando On

You need to use Q objects. Here is the example base on your code.

from django.db.models import Q

model1_obj = xx.objects.filter(date_generated__gte=1429142401, date_generated__lte=1431648001, ~Q(s__icontains="28"))

To exclude it, use exclude query. Your can read the details here. The code will be like this.

from django.db.models import Q

model1_obj = xx.objects.filter(date_generated__gte=1429142401, date_generated__lte=1431648001).exclude(~Q(s__icontains="28"))
0
blackwindow On

I have implemented it by following: model1_obj = xx.objects.filter( date_generated__gte=1429142401, date_generated__lte=1431648001, s__icontains="28").exclude(Q(u__in=[93,95]) | Q(v__isnull=True))