unagimiyagi
unagimiyagi

Reputation: 31

Intersection of two querysets in django

I can't do an AND on two querysets. As in, q1 & q2. I get the empty set and I do not know why. I have tested this with the simplest cases. I am using django 1.1.1

I have basically objects like this:

item1
   name="Joe"
   color = "blue"
item2
   name="Jim"
   color = "blue"
   color = "white"
item3
   name="John"
   color = "red"
   color = "white"

Is there something weird about having a many-to-many relationship or what am I missing?

queryset1 = Item.objects.filter(color="blue")
this gives (item1, item2)

queryset2 = Item.objects.filter(color="white")
this gives (item2, item3)

queryset1 & queryset2 gives me the empty set []
The OR operator works fine (I'm using "|" )

Why is this so?

Upvotes: 3

Views: 2612

Answers (2)

Yaroslav
Yaroslav

Reputation: 2736

Item.objects.filter(color="blue").filter(color="white")

Upvotes: 1

John Mee
John Mee

Reputation: 52313

qs = Item.objects.filter(color__in=['blue','white'])

Upvotes: 3

Related Questions