Hi Massimo, this is a very promising feature ! My quick two cents: Why not turn this generic by replacing the 'body' field with a 'node_name' field and add an attribute node table ? For example:
db.define_table('node', Field('parent_id','reference node'), Field('node_name','string')) and db.define_table('attribute', Field('node_id','reference node'), Field('key','string'), Field('value','string')) The tree definition above needs to create a fake root node, forces a given node to belong to only one parent node and is efficient when traversing tree from leaf to trunk but for a given node the children node(s) are unknown. The tree definition below could be a better solution, but may require extra work to keep references integrity especially when moving or deleting nodes: db.define_table('node', Field('parent_id','list:reference node'), Field('child_id','list:reference node'), // sorting this may permit to manage siblings Field('node_name','string')) Eventually a delirious attribute table evolution might be to have a type field (string, integer, reference, ....) and validator per node attribute, but I can't figure out how to do that :) Thanks again for this new feature, Mirko, PS: btw, no plans for DAL ZODB support ? http://www.zodb.org/en/latest/documentation/guide/introduction.html On Monday, October 21, 2013 5:57:00 AM UTC+2, Massimo Di Pierro wrote: > > It is a recurrent problem that is displays tree-like structures like > threaded comments. For example: > > db.define_table('post', > Field('parent_id','reference post'), > Field('body')) > > where each comment has a body and a parent_id (parent_id==None for the > root comment(s)) > We can populate the comments with some dummy data: > > def make_up_data(): > import random, uuid > ids = [None] > for k in range(100): > ids.append(db.post.insert(parent_id=random.choice(ids), > body=str(uuid.uuid4()))) > if k==0: > ids.pop(None) > if db(db.post).isempty(): make_up_data() > > The new feature in trunk allows you to select the comments are organized > them into trees. > > roots = db(db.post).select().as_trees() > > This returns a list of parent nodes. Each not stores its own children, > recursively. > > Now you can print them all using a tree traversal: > > def show(row,n=0): > return ' '*n+row.body+'\n'+''.join(show(c,n+1) for c in > row.children) > print show(roots[0]) > > Notice you can specify the name of the parent field: > > roots = db(db.post).select().as_trees(parent_name="parent_id") > > Please let me know if you think this can be improved. > > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.