How to make two internal joins in a query set 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 execute a query where I can get all 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 help how to do this. I found a similar question here, but it's too hard to understand for beginners like me. Thnak you so much already for any help.

+5
source share
1 answer

Something like that

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

or

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

Source: https://habr.com/ru/post/1234345/


All Articles