Il giorno ven, 23/05/2008 alle 09.20 +0200, Mailing List SVR ha scritto: > Hi, > > I have a database with the following structure: > > id name sublevel > > for example > > 1 Node1 None > 2 Node2 1 > 3 Node3 2 > 4 Node4 None > 5 Node5 1 > 6 Node6 5 > .... > .... > > > where Node1 and Node4 are treeview's master nodes, Node2 is a subnode of > Node1, Node3 is a subnode of Node2, Node5 is a subnode of Node1 and > Node6 a subnode of Node5 and so on > > I need a recursive function to populate treeview and I'm stuck on this > function :confused: > > I need a list where I have the master node with child node inside and if > a child node as others child nodes, I need thess nodes inside the child > nodes. > > Something like this: > > [ > {"id":1,"name":"Node1","Childs":[{"id:2","name":"Node2","Childs":[{"id":3,"name":"Node3","Childs":[]}]}, > {"id":5, > "name":"Node5","Childs":[{"id":6,"name":"Node6","Childs":[]}]}]},{"id":4,"name":"Node4","Childs":[]} > > ] > > I'm using django so I access the data as > > all=Data.objects.all() > > and I can do something like > > for a in all: > print a.id > print a.name > > to get database value. > > Any help is appreciated, > > thanks, > Nicola > > -- > http://mail.python.org/mailman/listinfo/python-list
only for the records attacched is a working solution, maybe not the best one but a working one, regards Nicol
#!/usr/bin/python # -*- coding: iso-8859-1 -*- #sys.path.append('/var/www/videosorveglianza') import sys sys.path.append('/home/nicola/django/videosorveglianza') from django.core.management import setup_environ import settings setup_environ(settings) from django.conf import settings from videosorveglianza.commands.Classi import * from videosorveglianza.commands.models import * zones=[] def ottienifigli(zona): zone=Zone.objects.filter(sublivello_di=zona) figli=[] for z in zone: figli.append({ 'Zona_id':z.id, 'Id_padre':z.sublivello_di.id, }) ottienifigli(z) zones.append({ 'Zona_id':zona.id, 'Figli':figli }) def rimuoviduplicati(lista): for s in lista: j=-1 i=0 for s1 in lista: j=j+1 if s['Zona_id']==s1['Zona_id']: i=i+1 if i >1: del lista[j] listaid=[] def generaid(): zone=Zone.objects.all() # se il sublivello e' nullo allora e' una zona padre for zona in zone: #if zona.sublivello_di is None: listaid.append(zona.id) #print padri padri=[] def generapadri(): zone=Zone.objects.all() # se il sublivello e' nullo allora e' una zona padre for zona in zone: if zona.sublivello_di is None: padri.append(zona.id) #print padri def visitaalbero(): zone=Zone.objects.all() for z in zone: ottienifigli(z) rimuoviduplicati(zones) print zones visitaalbero() generaid() generapadri() def accoppia(z): if z['Zona_id'] in listaid: i=-1 for c in z['Figli']: i=i+1 for z1 in zones: if c['Zona_id'] ==z1['Zona_id']: z['Figli'][i]=z1 accoppia(z['Figli'][i]) #print "\n\n\n\n" #print zones def accoppianodi(): for z in zones: accoppia(z) darimuovere=[] def childdarimuovere(): i=-1 for z1 in zones: i=i+1 if z1['Zona_id'] not in padri: darimuovere.append(i) accoppianodi() print "\n\n\n\n" print zones print "\n\n\n\n" childdarimuovere() print darimuovere k=-1 for s in darimuovere: k=k+1 del zones[s-k] print s #print type(s) print "\n\n\n\n" print zones
-- http://mail.python.org/mailman/listinfo/python-list