我有模特:
class Model_1(models.Model):
name = models.CharField(...)
class Model_2(models.Model):
[...]
m1 = models.ManyToManyField(Model_1, blank=True, null=True)
class Model_3(models.Model):
[...]
m1 = models.ForeignKey(Model_1)
我希望简化所有Model_3对象。到目前为止,我这样做了:
obj = Model_2.objects.filter(...)
this_i_want = []
for my2 in obj:
for my1 in my2.m1.all():
objects = Model_3.objects.filter(m1=my1)
for o in objects:
this_i_want.append(o)
如何做到最简单?我必须从Model_2开始。
答案 0 :(得分:1)
试试这个:
Model_3.objects.filter(m1__model_2__in=Model_2.objects.all())