Django ORM

系统教程 行业动态 更新时间:2024-06-14 17:01:34
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

更多推荐

本文发布于:2023-04-20 18:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/94e9d7fa02112ae4d28a0d48bd08de5d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   ORM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!