I have these models:
class A(Model):
pass
class B(Model):
the_a = ForeignKey(A)
class C(Model):
the_b = ForeignKey(B)
somewhere in code, I have an A instance. I know how to get all of the B’s associated with A; A.b_set.all() – but is there a way to get all of the C’s associated with all the B’s associated with my A, without doing potentially many queries?
You can execute this query to get all the C’s associated via B’s to your A:
where
instance_of_ais some instance of class A.For more information, see http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships