4

考虑到以下型号,知道一个家庭,我如何选择没有买家的孩子?

class Family...

class Kid(models.Model):
    name = models.CharField(max_length=255)
    family = models.ForeignKey(Family)
    buyer = models.ManyToManyField(Buyer, blank=True, null=True)

family = get_object_or_404(Family, pk=1)
for_sale = family.kid_set.filter(buyer... this screws my child trade business
4

2 回答 2

8

family.kid_set.filter(buyer__isnull=True)应该管用。

于 2010-10-04T15:04:27.663 回答
3

@piquadrat 的回答是正确的。你也可以这样做:

for_sale = Kid.objects.filter(family__pk = 1, buyer = None)

这使您可以避免单独查询来查找Family实例。

于 2010-10-04T15:14:27.337 回答