Hello, here is what I did: db.define_table('article', Field('f_parent','reference article'), Field('title'))
###### def index(): rows = dict((r.id,r) for r in db(db.article).select()) for id,row in rows.items(): row.children=[] for id,row in rows.items(): if row.f_parent==1: root=row else: rows[row.f_parent].append(row) def tree(row): return DIV(H1(row.title),UL(*[LI(tree(child)) for child in row.children])) return tree(root) ################## Here are the articles article.id article.f_parent article.title 1 1 Racine 2 1 Branche1 3 1 Branche2 It gives me the following error : Traceback (most recent call last): File "C:\Documents and Settings\sylvain\Bureau\web2py2\gluon \restricted.py", line 188, in restricted exec ccode in environment File "C:/Documents and Settings/sylvain/Bureau/web2py2/applications/ test/controllers/default.py", line 66, in <module> File "C:\Documents and Settings\sylvain\Bureau\web2py2\gluon \globals.py", line 96, in <lambda> self._caller = lambda f: f() File "C:/Documents and Settings/sylvain/Bureau/web2py2/applications/ test/controllers/default.py", line 60, in index else: rows[row.f_parent].append(row) File "C:\Documents and Settings\sylvain\Bureau\web2py2\gluon \sql.py", line 742, in __getattr__ return dict.__getitem__(self,key) KeyError: 'append' T tried to modify : else: rows[row.f_parent].append(row) with else: rows[row.children].append(row) as it seems we're trying to build a list of children. Yet i get another error : TypeError: list objects are unhashable Can anybody help me ? On 29 nov, 12:27, JmiXIII <sylvn.p...@gmail.com> wrote: > That's it I'll try it > thanks a lot! > > On 29 nov, 00:48, mdipierro <mdipie...@cs.depaul.edu> wrote: > > > I assume you are trying to build a hierarchial tree of articles and > > you want them all from the parent. > > > I would do this something like this: > > > db.define_table('article',Field('f_parent','reference > > article'),Field('title')) > > > def index(): > > rows = dict((r.id,r) for r in db(db.article).select()) > > for id,row in rows.items(): row.children=[] > > for id,row in rows.items(): > > if row.f_parent==0: root=row > > else: rows[row.f_parent].append(row) > > def tree(row): > > return DIV(H1(row.title),UL(*[LI(tree(child)) for child in > > row.children]))) > > return tree(root) > > > On Nov 28, 5:00 pm, JmiXIII <sylvn.p...@gmail.com> wrote: > > > > Hello, > > > > I'm facing I guess a well known programming problem. It is not really > > > related to web2by, but since I'm using web2py I hope somebody here can > > > help. > > > > Here is my controller : > > > > def global_view(): > > > parents = db(~(db.t_article.id==db.t_list.f_article)).select() > > > tree={} > > > for parent in parents: > > > tree[parent.t_article.id]={} > > > sublevel=db(parent.t_article.id==db.t_list.f_parent).select() > > > for son in sublevel: > > > tree[parent.t_article.id][son.f_article]=son.f_article > > > tree[parent.t_article.id][son.f_article]={} > > > sublevel2=db(son.f_article==db.t_list.f_parent).select() > > > for son2 in sublevel2: > > > tree[parent.t_article.id][son.f_article] > > > [son2.f_article]=son2.f_article > > > > > > tree[parent.t_article.id][son.f_article][son2.f_article]={} > > > return dict(tree=tree) > > > > So in fact I'd like to go on for son3, son4, based on the same model > > > as sublevel2... until sublevelx=None. > > > I guess I should use some kind of recursive or loop function. > > > I've read different things using right and left variable for > > > hierarchical tree, but it seems to me that it is possible to build a > > > loop in my controller without modifying my model. > > > > Hope I do not disturb this groupes with this kind of question. > >