B
B
Boldy2014-11-09 12:35:18
Django
Boldy, 2014-11-09 12:35:18

How to get children of level n MPTTModel in django?

I am building a tree in django, connected mptt. The task is as follows: get all children at level n of their subtree (grandchildren-great-grandchildren). get_children().all(), as I understand it, returns children of the first level. We also need grandparents. I use self.parent.parent.parent... but I suspect there is a more concise way to achieve the result. We need instances / arrays of instances of the TreeNode class

class TreeNode(MPTTModel):
    parent = TreeForeignKey('self', null=True, blank=True, related_name='child')
    ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2014-11-10
@Boldy

Select among children
Select among parents
node.get_ancestors().filter(level=n)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question