我有一个带有自引用映射backref'parent'的类Node,它表示SQLAlchemy中的一棵树,我想选择整个树。
Class Node(db.Model):
id = db.Column(db.Integer, primary_key= True)
name = db.Column(db.String(100), nullable= False)
parent_id = db.create_all(db.Integer, db.ForeignKey('id'))
parent = db.relationship("Node",
primaryjoin='node.c.id ==
node.c.parent_id',
remote_side='Node.id',
backref=db.backref("node"))
Id |姓名| parent_id
1 | 111 |没有
2 | 222 |没有
3 | 333 | 2
4 | 444 | 2
5 | 555 | 2
6 | 666 | 5
7 | 777 |无
from collections import defaultdict
from sqlalchemy.orm.attributes import set_committed_value
nodes = session.query(Node).all()
# Collect parent-child relations
children = defaultdict(list)
for node in nodes:
if node.parent:
children[node.parent.id].append(node)
# Set collected values
for node in nodes:
set_committed_value(node, 'children', children[node.id])
使用set_committed_value,获取结果
defaultdict(<type 'list'>,
{
<Node 1>: [],
<Node 3>: [],
<Node 4>: [],
<Node 6>: [],
<Node 5>: [<Node 6>],
<Node 2>: [<Node 3>, <Node 4>, <Node 5>],
<Node 7>: []
}
)
我希望结果是
[
{
"id":1,
"name":111
},
{
"id":2,
"name":222,
"children":[
{
"id":3,
"name":333
},
{
"id":4,
"name":444
},
{
"id":5,
"name":555,
"children":[
{
"id":6,
"name":666
}
]
}
]
},
{
"id":7,
"name":777
}
]
有什么解决办法吗?