考虑到以下型号,了解一个家庭,如何选择没有买家的孩子?
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
答案 0 :(得分:8)
family.kid_set.filter(buyer__isnull=True)
应该有用。
答案 1 :(得分:3)
@ piquadrat的answer是正确的。你也可以 做:
for_sale = Kid.objects.filter(family__pk = 1, buyer = None)
这使您可以避免单独的查询来查找Family
实例。