1👍
You could use isnull
or any other field lookups to constrain data from A:
q = A.objects.filter(b__isnull=False, b__c__parameter1__isnull=False)
result query will be similar to this:
SELECT "appname_A"."id", "appname_A"."parameter1" FROM "appname_A"
INNER JOIN "appname_B" ON ("appname_A"."id" = "appname_B"."a_id")
INNER JOIN "appname_C" ON ("appname_B"."id" = "appname_C"."b_id")
WHERE ("appname_C"."parameter1" IS NOT NULL AND "appname_B"."id" IS NOT NULL)
ORDER BY "appname_A"."id" ASC
and you should use select_related
from C
(backwards) if you want to select all data in one query from joined tables:
q = C.objects.filter(b__a__parameter1=SOMETHING).select_related()
result query will start from:
SELECT "appname_C"."id", "appname_C"."parameter1", "appname_C"."b_id",
"appname_B"."id", "appname_B"."parameter1", "appname_B"."a_id",
"appname_A"."id", "appname_A"."parameter1"
INNER JOIN ...
👤ndpu
Source:stackexchange.com