如何在django中查询SELECT WHERE EXISTS ... [英] How to query SELECT WHERE EXISTS... in django

查看:130
本文介绍了如何在django中查询SELECT WHERE EXISTS ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django模型的问题,我不知道如何创建一个查询...

I have a problem with django models, I don't know how to create a query...

我有三个类/模型:


  • A只有名称(和id)

  • B具有A的名称,ID和外键

  • C在其他字段中有B的外键。

所以每个C都有它的A A可以有更多的As)但是没有直接的外键 - 它必须经过B。

So every C has its A (while every A can have more As) but doesn't have its foreign key directly - it has to go through B.

我有一个Cs的子集被另一个查询。现在我想选择这样的As,在子集中有一个C。我该怎么办?

I have a subset of Cs got by another query. Now I want to select such As for which there is a C in the subset. What do I do?

推荐答案

假设 c_subset C 您拥有的对象,以获得 A ,其中包含这些 C s
你可以做

Assuming c_subset is subset of C objects that you have, to get As which has these Cs you can do

>>> A.objects.filter(b__c__in = c_subset)

这篇关于如何在django中查询SELECT WHERE EXISTS ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆