Query Q() is not working as 'set contain' for related field in Django?
<p>Let's say we have baskets of fruits. How to filter out those baskets that contains all fruits in a given basket? </p>

<p>In this document <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#in" rel="nofollow">https://docs.djangoproject.com/en/dev/ref/models/querysets/#in</a> 
'in' method seems will return any basket which contains any of the given fruit. 
Is there any "set_contains" method to be used for filtering ? Such as Basket.objects.filter(fruit_set_containsAll=fruitSet) ?</p>

<p>Edit: I found that Q() is <strong>not working</strong> as expected. I'll post the test here:</p>

<pre><code>class Basket(models.Model):
    weight = models.FloatField(default=1)
class Fruitname(models.Model):
    name = models.CharField(max_length=32)
class Fruit(models.Model):
    ofkind = models.ForeignKey(Fruitname, on_delete=models.CASCADE)  
    inbasket = models.ForeignKey(Basket, on_delete=models.CASCADE)  
    weight = models.FloatField(default=1)
</code></pre>

<p>the database is set as 'apple' only in 1 basket, 'pear' in 2 baskets, and 'banana' in 3 basket:</p>

<pre><code>print Basket.objects.all()
print Fruitname.objects.all()
[&lt;Basket: id 31 has 4 fruits 
    1. id - 53 apple(28), weight 1.00
    2. id - 54 apple(28), weight 2.00
    3. id - 55 apple(28), weight 3.00
    4. id - 62 banana(30), weight 10.00
&gt;, &lt;Basket: id 32 has 2 fruits 
    1. id - 56 pear(29), weight 4.00
    2. id - 57 banana(30), weight 5.00
&gt;, &lt;Basket: id 33 has 4 fruits 
    1. id - 58 pear(29), weight 6.00
    2. id - 59 banana(30), weight 7.00
    3. id - 60 pear(29), weight 8.00
    4. id - 61 pear(29), weight 9.00
&gt;]
[&lt;Fruitname: apple(28)&gt;, &lt;Fruitname: pear(29)&gt;, &lt;Fruitname: banana(30)&gt;]
</code></pre>

<p>If I try to query with 'apple' and 'banana', it gives empty set !!</p>

<pre><code>print Basket.objects.filter(Q(fruit__ofkind__name__in=['apple'])&amp;Q(fruit__ofkind__name__in=['banana'])).distinct()
[]
</code></pre>

<p>similarly, </p>

<pre><code>print Basket.objects.filter(Q(fruit__ofkind__name__in=['banana'])&amp;Q(fruit__ofkind__name__in=['pear'])).distinct()
[]
</code></pre>

<p>Here's how I use 'in' to filter, it is not what I need which is supposed to be an empty set.</p>

<pre><code>print Basket.objects.filter(Q(fruit__ofkind__name__in=['apple','pear'])).distinct()
[&lt;Basket: id 31 has 4 fruits 
    1. id - 53 apple(28), weight 1.00
    2. id - 54 apple(28), weight 2.00
    3. id - 55 apple(28), weight 3.00
    4. id - 62 banana(30), weight 10.00
&gt;, &lt;Basket: id 32 has 2 fruits 
    1. id - 56 pear(29), weight 4.00
    2. id - 57 banana(30), weight 5.00
&gt;, &lt;Basket: id 33 has 4 fruits 
    1. id - 58 pear(29), weight 6.00
    2. id - 59 banana(30), weight 7.00
    3. id - 60 pear(29), weight 8.00
    4. id - 61 pear(29), weight 9.00
&gt;]
</code></pre>

<p>The only way that is working properly is chaining with filter:</p>

<pre><code>Basket.objects.filter(fruit__ofkind__name__in=['apple']).filter(fruit__ofkind__name__in=['banana']).distinct()
[&lt;Basket: id 31 has 4 fruits 
    1. id - 53 apple(28), weight 1.00
    2. id - 54 apple(28), weight 2.00
    3. id - 55 apple(28), weight 3.00
    4. id - 62 banana(30), weight 10.00
&gt;]
</code></pre>

<p>These code is tested with Django 1.9.4
Any explaining? I would undo the accepted answer for the moment. </p>