-1

我有模型:

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 出发。

4

1 回答 1

1

试试这个:

Model_3.objects.filter(m1__model_2__in=Model_2.objects.all())
于 2012-07-18T00:04:33.453 回答