我正在构建一个具有三个不同级别的对象的 SQL Alchemy 结构;例如,考虑一个简单的数据库来存储有关一些博客的信息:有一些 Blog
对象、一些 Post
对象和一些 Comment
对象。每个Post
都属于一个Blog
,每个Comment
都属于一个Post
。使用 backref
我可以自动获得属于 Blog
的所有 Post
的列表,对于 Comment
也是如此。
我起草了一份skeleton for such a structure .
我现在想做的是直接在 Blog
中拥有属于该 Blog
的所有 Comment
的数组。我尝试了几种方法,但它们不起作用,甚至让 SQL Alchemy 以我无法修复的方式哭泣。我认为我的需求是相当频繁的,但我找不到任何有用的东西。
有人建议我该怎么做吗?
谢谢。
最佳答案
我认为你可以使用 association proxy
class Blog(Base):
comments = association_proxy('posts', 'comments')
关于python - SQLAlchemy : Relationship with grandson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6469678/