I've simplified the models for the question, but essentially I'm trying to perform a query with the following models:
class mod1(models.Model):
mod1_id = CharField(unique=True, ...)
class mod2(models.Model):
field2 = models.ManyToManyField(mod1)
So any mod2
object can have x number of mod1s...in this case I have a mod2
object that has four mod1
objects attached to it with the mod1_ids
of "foo", "bar", "fizz", and "bang". If I have two different lists of mod1_ids, can I make separate Q objects of the lists to perform the filter so it returns my mod2 object? I've tried it and so far it hasn't yielded anything. Assume I can't combine the lists of ids to form one list. What I've tried:
from django.db.models import Q
Q1 = Q(field2__mod1_id__in=["foo"])
Q2 = Q(field2__mod1_id__in=["bar", "fizz"]
len(mod2.objects.filter(Q1 & Q2))
>>> 0
When I perform those queries individually, they both work, but together they don't. Can someone please help me out here? Thank you.