How to do an OR on a Django ManyToManyField without getting duplicates?
<p>I have the following two Django Classes <code>MyClassA</code> and <code>MyClassB</code>.</p>

<p><code>MyClassB</code> has a many-to-many field of <code>MyClassA</code>s</p>

<pre><code>from django.db import models

class MyClassA(models.Model):
    name = models.CharField(max_length=50, null=False)

    def __str__(self):
        return "&lt;%s %s&gt;" % (
        self.__class__.__name__, "; ".join(["ID: %s" % self.pk, "name: %s" % self.name, ]))

class MyClassB(models.Model):
    name = models.CharField(max_length=50, null=False)
    my_class_as = models.ManyToManyField(MyClassA, related_name="MyClassB_my_class_as")

    def __str__(self):
        return "&lt;%s %s&gt;" % (
        self.__class__.__name__, "; ".join(["ID: %s" % self.pk, "name: %s" % self.name, ]))
</code></pre>

<p>Then I seed the database with the following data:</p>

<pre><code>&gt;&gt;&gt; a = MyClassA.objects.create(name="A")
&gt;&gt;&gt; b = MyClassA.objects.create(name="B")
&gt;&gt;&gt; c = MyClassA.objects.create(name="C")
&gt;&gt;&gt; d = MyClassA.objects.create(name="D")
&gt;&gt;&gt; e = MyClassA.objects.create(name="E")
&gt;&gt;&gt; u = MyClassB.objects.create(name="U")
&gt;&gt;&gt; u.my_class_as.add(c)
&gt;&gt;&gt; u.my_class_as.add(d)
&gt;&gt;&gt; u.save()
</code></pre>

<p>I want to find the (unique) list of MyClassBs that have a relationship with a instance <code>c</code> or <code>d</code> of MyClassA. How can I do it??</p>

<p>My solution was to create the following static method on <code>MyClassB</code>:</p>

<pre><code>@staticmethod
def is_m2m_related_queryset(my_class_a_list):
    q = Q()
    for curr_a in my_class_a_list:
        q |= Q(my_class_as=curr_a)
    return q
</code></pre>

<p>...And then run the following query:</p>

<pre><code>&gt;&gt;&gt; MyClassB.objects.filter(MyClassB.is_m2m_related_queryset([c, d]))
[&lt;MyClassB: &lt;MyClassB ID: 11; name: U&gt;&gt;, &lt;MyClassB: &lt;MyClassB ID: 11; name: U&gt;&gt;]
</code></pre>

<p>But wait! 
Instead of returning to me a singleton list containing the <code>u</code> object as I expected, this queryset returns <code>[u, u]</code>. That's not what I want. 
Shouldn't this return <code>[u]</code>?? 
Why did it return what it did? </p>

<p>How should I get what I want? Should I simply append a <code>.distinct()</code> to my queryset? Or is there a better/more efficient for me construct my queryset?? </p>

<p><strong>EDIT:</strong></p>

<p>I tried Andrey's solution suggested below (and a variant thereof), but that didn't work either:</p>

<pre><code>&gt;&gt;&gt; MyClassB.objects.filter(my_class_as__pk__in=[c.pk, d.pk])
[&lt;MyClassB: &lt;MyClassB ID: 1; name: U&gt;&gt;, &lt;MyClassB: &lt;MyClassB ID: 1; name: U&gt;&gt;]

&gt;&gt;&gt; MyClassB.objects.filter(my_class_as__in=[c, d])
[&lt;MyClassB: &lt;MyClassB ID: 1; name: U&gt;&gt;, &lt;MyClassB: &lt;MyClassB ID: 1; name: U&gt;&gt;]
</code></pre>

<p>Have you tried:</p>

<pre><code>MyClassB.objects.filter(my_class_as__pk__in=[c.pk, d.pk])?
</code></pre>

python django orm many-to-many django-queryset