def search_child(documents, parent):
childs = {}
for document in documents:
if parent.id == document.parent_id:
childs.update({document: search_child(documents, document)})
return childs
documents = list(Document.objects.all())
tree = {document: search_child(documents, document) for document in documents if not document.parent_id}
if parent.id == document.parent_id:
Не срабатывает видимо.