1
我下面從數據庫中顯示分層數據下面的教程http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html#adjacency-list-relationships分組螺紋評論牽強從數據庫
到目前爲止,我有如下表
class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('node.id'))
data = Column(String(50))
parent = relationship("Node", remote_side=[id])
而且在MySQL以下條目
id parent_id data
1 NULL root
2 1 [->] child1
3 1 [->] child2
4 3 [->] subchild1
5 3 [->] subchild 2
6 1 [->] child3
7 NULL root2
8 NULL root3
9 7 [->] subchild0froot2
10 8 [->] subchildofroot3
11 1 [->] child4
我想檢索適合評論的格式的數據,例如root - > child1 - > child2 - >(subchild1-> subchild2) - > child 4
到目前爲止,我已經能夠通過這個查詢
nodealias = aliased(Node)
qry = session.query(nodealias,Node).\
join(nodealias, Node.parent).\
filter(and_(Node.postid==45))
print qry
for x,y in qry:
print x.data
print y.data
print "...."
And it displays
root
child1
....
root
child2
....
child2
subchild1
....
child2
subchild 2
....
root
child3
....
root
child4
....
我想組這導致以下方式
root
....
child1
....
child2
subchild1
subchild 2
....
child3
....
child4
....
我認爲這將太複雜,無法在mako中呈現 – Madawar
我不知道你想要在這背後真正實現什麼。你也許應該從SQL數據庫處理你的數據到一個合適的數據結構,然後渲染生成的結構...... – Kaltezar