1
我正在根據名爲Pages的db.Model
檢索的列表構建樹結構。遍歷頁面屬性sortIndex
每頁條目都有一個parentKey屬性,它是一個db.SelfReferenceProperty()
和一個叫做sortIndex的db.IntegerProperty()
。
我獲取列表並調用遍歷列表的方法,並將一個嵌套的字典作爲我的樹。我獲取整個列表的原因是我想跳過多個查詢。
pages = Pages.gql('ORDER BY sortIndex').fetch(1000)
build_tree(pages)
而且build_tree:
def build_tree(nodes, *args):
# create empty tree to fill
tree = {}
build_tree_recursive(tree, None, nodes, *args)
return tree
def build_tree_recursive(tree, parent, nodes, *args):
# find root children, first level nodes have no parentKey
if parent is None:
children = [n for n in nodes if n.parentKey == None]
# find children
else:
children = [n for n in nodes if n.parentKey is not None and n.parentKey.key() == parent]
# build a subtree for each child
for child in children:
# start new subtree
key = child.key()
# Use page entry key as unique dict key
tree[key] = { 'page' : child, 'children' : {}}
# call recursively to build a subtree for current node
build_tree_recursive(tree[key]['children'], key, nodes)
的問題是,在列表中選擇獲取的重新安排,不照DET ORDER BY。我認爲這是由於當找到合適的父代時每個頁面都放在列表中。但即使是第一級(網頁有parentKey == None
)得到的返回錯誤的順序。我已經嘗試在樹[str(i)+'_'+ str(key)]上使用循環計數器設置前綴,但仍然沒有以正確的順序返回。
那麼問題如何讓他們在正確的順序?
EDIT [解決]:
參見下面
要知道,你沒有節省查詢這裏 - 每當你首次對實體做「n.parentkey」的時候,它提取實體來自數據存儲。 – 2010-09-14 16:13:52
謝謝。我在過去的幾個小時裏一直在研究這個問題,並且確實注意到了它。任何想法如何解決它?我想我在某處可以預先獲得一些數據? – fredrik 2010-09-14 18:20:15
@fredrik:如果你已經回答了你自己的問題而不是編輯它(甚至可能將其標記爲已接受),其他人在搜索舊的,沒有答案的問題時不會一次又一次地找到你的問題。 – Anthon 2013-04-07 15:17:19