Suppose I have the following Django models:
class myClass1(models.Model):
myField1 = models.IntegerField()
class myClass2(models.Model):
myLocalClass1 = models.ManyToManyField(myClass1)
Furthermore, suppose I have a list of unique myClass1s:
a = myClass1(myField=1)
b = myClass1(myField=2)
c = myClass1(myField=3)
myTargetList = [a, b, c]
Now, I would like to write a Django query using Q objects such that it returns all the myClass2s that have any member of myTargetList as myLocalClass1. Furthermore, I don't know the exact size of myTargetList in advance.
How should I do it? This obviously won't work:
myClass2.objects.filter(Q(myLocalClass1__in=myTargetList))