这是我的模型的一个例子:
class Section(models.Model):
name = models.CharField(max_length=255)
class Album(models.Model):
name = models.CharField(max_length=255)
model1 = models.ManyToManyField(Section, on_delete=models.CASCADE,
related_name='model2')
class Image(models.Model):
name = models.CharField(max_length=255)
active = models.BooleanField(default=True)
model2 = models.ManyToManyField(Album, on_delete=models.CASCADE,
related_name='model3')
我试图找到所有具有活动字段= False的图像的专辑的部分
Section.object.filter(albums__images__active=False)
但即使他们的字段为活动= True,我也会获得所有图像
无论如何,你的模型缺乏可读性,所以我假设你的模型是下面的,
class Section(models.Model):
name = models.CharField(max_length=255)
class Album(models.Model):
name = models.CharField(max_length=255)
genre = models.ManyToManyField(Section, on_delete=models.CASCADE, related_name='genre')
class Image(models.Model):
name = models.CharField(max_length=255)
active = models.BooleanField(default=True)
field_new = models.ManyToManyField(Album, on_delete=models.CASCADE, related_name='field_new')
如果是这样,可能的答案是,
section_queryset = Section.objects.filter(genre__isnull=False, genre__field_new__isnull=False, genre__field_new__active=False)