简体   繁体   English

如何在Django中的查询集中执行两个内连接

[英]How to perform two inner joins in a queryset in Django

I have three models. 我有三个型号。

class A(models.Model):
    status = models.PositiveSmallIntegerField()
    status_time = models.DateTimeField(auto_now_add=True)
    b = models.ForeignKey(B)
    d=models.ForeignKey(D)

class B(models.Model):
    ***other Fileds*****
    c = models.OneToOneField(C)
    ***other fields***

class C(models.Model):
    c_name = models.CharField(max_length=9,unique=True)

I want to perform a query where i can get all the c_name. 我想执行一个查询,我可以获得所有的c_name。

SELECT * FROM A inner join B  on A.b_id=B.id inner join 
C  on B.c_id=C.id where A.d_id=1 and A.status=6

can anyone please help how to do. 谁能请帮忙怎么做。 i found a similar question here, but it is too much complex to understand for a newbie like me. 我在这里发现了一个类似的问题,但对于像我这样的新手来说理解起来太复杂了。 Thnak you so much already for any help. 为了任何帮助,你已经非常了解你。

Something like this 像这样的东西

C.objects.filter(b__a__status=6, b__a__d=1).values_list('c_name', flat=True).distinct()

or this 或这个

A.objects.filter(status=6, d=1).values_list('b__c__c_name', flat=True).distinct()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM