2

I want to gain and OR operation in my exclude statement.

Like:

Select all those records where name is NOT Mason and Status is NOT Unactive

OR

Select all those records where name is NOT Mason and Status is active

Tried.

Users.objects.exclude(name="Mason", active=False)

But this is not an OR operation but AND. How to get OR operation.

1
  • Actually you have to use exclude twice to get this. Commented Apr 10, 2014 at 9:27

2 Answers 2

3

In order to use exclude with OR opration. you have to use exclude twice.

Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3)).exclude(headline='Hello')

OR

User.objects.exclude(name="Mason").exclude(active=False)

Reference

Docs

Sign up to request clarification or add additional context in comments.

Comments

2

Read about complex lookups with Q objects doc. For your case:

from django.db.models import Q

q1 = Q(name="Mason")
q2 = Q(active=False)

Users.objects.exclude( q1 | q2 )

Or, more closed tou your SQL

#where name is NOT Mason and Status is NOT Unactive
Users.objects.filter( ~q1 & ~q2 )

#where name is NOT Mason and Status is active
q2 = Q(active=True)
Users.objects.filter( ~q1 & q2 )

1 Comment

Worked without any problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.