Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a unilevel Multi Level Marketing (MLM) application which stores data with mysql columns id, parent_id

with each item having one parent. each item can have up to 50 children. Given a particular id, how do I get all its children and children of its children etc. and its parents and parent's parents, etc. I want to get this data into a dic and display. Thanks

What I have tried:

def children(self):
        children_list = {}
        level_one = Member.objects.filter(parent=self)
        if level_one.count()>0 :
            for child in level_one:
                children = self.__children_of_child(child)
                children_list[child.id] = children
    
    def __children_of_child(self, parent):
        children_list = {}
        children = Member.objects.filter(parent=parent)
        if children.count()>0:
            for child in children:
                children_list[child.id] = self.__children_of_child(child)
            
        return children_list
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900