django filter on many to many along with Q
问题 class ModelA: m2m1 = Foreignkey relatedname = 'm2ms' We can get m2m1 objects using: modela_instance.m2ms.all() Sample output: for x in ModelA.objects.all(): print x.m2ms.all().values_list('id', Flat=True) 1,2,3 1, [] 2 4,5 So, if I want fetch all instances whose m2ms has ids 1,2,3 (can have more too). Then I can do this: ModelA.objects.filter(m2ms__id=1).filter(m2ms__id=2).filter(m2ms__id=3) How can I do this with Q ? ModelA.objects.filter(Q(some_boolean=True, simple_field=5) | Q(some_boolean