开发者

Django aggregate second-degree many-to-many relations

开发者 https://www.devze.com 2023-03-17 02:44 出处:网络
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 object

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())
0

精彩评论

暂无评论...
验证码 换一张
取 消