Note that there are some explanatory texts on larger screens.

plurals
  1. POTree traversal with page property sortIndex
    primarykey
    data
    text
    <p>I'm building a tree structure based on a list retrieved from a <code>db.Model</code> called Pages. </p> <p>Each Page entry has a parentKey property that's a <code>db.SelfReferenceProperty()</code> and a <code>db.IntegerProperty()</code> called sortIndex.</p> <p>I fetch the list and call a method to travers over the list and make a nestled dict as my tree. The reason I fetch the entire list is that I want to skip multiple queries. </p> <pre><code>pages = Pages.gql('ORDER BY sortIndex').fetch(1000) build_tree(pages) </code></pre> <p>And the build_tree:</p> <pre><code>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) </code></pre> <p>The problem is that the list get's rearranged and don't follow det ORDER BY. I think this is due to that each Page is put in the list when a proper parent is found. But even the first level (Pages that has <code>parentKey == None</code>) get's returned in the wrong order.</p> <p>I've tried setting a prefix using a loop counter on tree[str(i) + '_' + str(key)] but still didn't return in a proper order.</p> <p>So the question how to get them in an proper order?</p> <p><strong>EDIT [Solved]:</strong></p> <p>See below</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload