[Django]-Django exclude query, can i pass multiple parameters?

8👍

You can do this, but then you say exclude all Demos that have company__isnull=True, and as email 'abc@gmail.com'. This thus means that if only one of the two conditions is met, it is not excluded.

You can work with Q objects, like:

from django.db.models import Q

Demo.objects.exclude(
    Q(company__isnull=True) |
    Q(email='abc@gmail.com')
)

But it is likely more readable with:

Demo.objects.exclude(
    company__isnull=True
).exclude(
    email='abc@gmail.com'
)

this will thus exclude items for which company is NULL and exclude items for which email is 'abc@gmail.com'. So here we exclude it from the moment at least one condition is satisfied.

1👍

Queries are lazy, nothing happens until you try to pull data from it, so there’s no downside to using .exclude() more than once.

Demo.objects.exclude(
    company__isnull=True
).exclude(
    email='abc@gmail.com'
)

Another way is to use Q objects that can be combined using the & and | operators.

# Import
from django.db.models import Q

# Exclude query
exclude_query = Q()
exclude_query.add((~Q(company__isnull=True), Q.OR)
exclude_query.add((~Q(email="abc@gmail.com"), Q.OR)

# Get final objects which excludes email "abc@gmail.com" and null companys
result = Demo.objects.filter(exclude_query)

0👍

Your logic is not right, this is excluding every Demo that has no company and has an email of abc@gmail.com. You may want to chain exclude() calls instead.

Demo.objects.all().exclude(company__isnull=True).exclude(email="abc@gmail.com")

Leave a comment