Django aggregate second-degree many-to-many relations
Apologies for the difficult title, but I essentially have a model Foo that has multiple Bar objects (i.e. Foo.bar_set.all() gives me all the Bar objects). Also, each Bar object has multiple Baz objects in the same manner.
Is there any way to access all the Baz objects that belong to the Bar objects of Foo? Something like Foo.bar_set.all().baz_set.all() would be convenient...
As of now I'm just loading Foo.bar_set.all(), i开发者_如何转开发terating through the bar's, and aggregating a set of foo's. Is there a better way to do this?
Maybe something like this? Here I'm assuming Baz has a field bar which is a ForeignKey to Bar:
Baz.objects.filter(bar__in=foo.bar_set.all())
加载中,请稍侯......
精彩评论