Django ORM - 涉及3个模型的ForeignKey(Django ORM - ForeignKey with 3 models involved)
我有以下型号:
class A(models.Model): ... class B(models.Model): a = models.ForeignKey(A) c = models.ForeignKey(C, null=True, blank=True) class C(models.Model): ...我想得到所有有任何B的A,有任何C = null。 我试过了
A.objects.filter(b__c__isnull=True)但这显然不能取得正确的结果。
我错过了什么吗?
I have the following models:
class A(models.Model): ... class B(models.Model): a = models.ForeignKey(A) c = models.ForeignKey(C, null=True, blank=True) class C(models.Model): ...I would like to get all A's that have any B, that have any C = null. I have tried doing
A.objects.filter(b__c__isnull=True)but that isn't fetching the correct results apparently.
Am I missing something?
最满意答案
尝试使用a.b_set.filter(c__isnull=True) ,首先我们要求模型A,然后我们要求A中的对象B但是用c == Null过滤
Try with a.b_set.filter(c__isnull=True) , first we ask for the model A, then we ask for the objects B inside A but filtered with c == Null
更多推荐
发布评论