Python treeview and recursion: help needed

Discussion in 'Python' started by Mailing List SVR, May 23, 2008.

  1. 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
     
    Mailing List SVR, May 23, 2008
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VHltYm93?=
    Replies:
    2
    Views:
    25,261
    =?Utf-8?B?VHltYm93?=
    Jan 10, 2006
  2. Newbie
    Replies:
    5
    Views:
    625
    andreas
    Oct 4, 2004
  3. JimC
    Replies:
    3
    Views:
    651
  4. Allan W
    Replies:
    4
    Views:
    708
    Jos A. Horsmeier
    Jan 22, 2004
  5. abargaddon
    Replies:
    0
    Views:
    1,264
    abargaddon
    Jan 17, 2008
  6. Ryan Macy

    Ideas needed & help needed!

    Ryan Macy, Jul 19, 2006, in forum: Ruby
    Replies:
    2
    Views:
    719
    Ryan Macy
    Jul 19, 2006
  7. James K.
    Replies:
    2
    Views:
    252
    James K.
    May 25, 2011
  8. Replies:
    8
    Views:
    1,165
    John Reye
    Apr 26, 2012
Loading...