my django application stores some hierarchical data in the DB, as following:
class Foo(Model):
# ... fields
parent = ForeignKey('self', null = True)
Now, in order to display that tree data in jqGrid Tree I need that data to be converted to adjacency list or nested set, as those two methods are supported by jqGrid.
I've written simple function that builds an adjacency list, as following (pseudocode)
list=[]
def build_list(parent = None, level = 0)
data = Foo.objects.select_related().filter(parent=parent).annotate(sub_count = Count('foo'))
for x in objects:
obj = {
'name': x.name,
'id': x.pk,
'level': level,
'isLeaf': x.sub_count == 0
}
list.append(obj)
if x.sub_count > 0:
build_list(x.pk, level + 1)
however I'm afraid of this recursion as the number of records grows.
Is there any better way of doing that?
PS. I can't change the schema (definition) of the model, as other parts of the application (and other services) rely on the current structure.
PS. 2. the database behind is Postgres, but I'd like the solution to stay db-agnostic, if possible