Reputation: 5850
Suppose I have:
Class Level1:
name = CharField()
Class Level2:
name = CharField()
level1 = ForeignKey(Level1)
Class Level3:
name = CharField()
level2 = ForeignKey(Level2)
Class Level4:
name = CharField()
level3 = ForeignKey(Level3)
Suppose I already have an Level1 object: level1, how can get all Level4 objects of Level1?
Like the meaning: level1.level2_set.level3_set.level4_set.
Upvotes: 8
Views: 746
Reputation: 599778
You start with the model whose objects you want to get, Level4, then follow the relationships with the double-underscore syntax.
Level4.objects.filter(level3__level2__level1=my_level1_object)
Upvotes: 10
Reputation: 1619
Based on the docs, I'd use:
Level4.objects.filter(name="...", level3__name="...", level3__level2__name="...", level3__level2__level1__name="...")
Upvotes: 0