Question

I would like to know a thing about django-mptt, as I'm considering using it for a engineering coordination project.

I would have a Model class (say, 'Interface'), with a ForeignKey to the Model (say, 'Location') in the Tree.

Will I be able to filter Interfaces by the depth of the Location they refer to ? The depth being the number of parents.

I don't know yet how to organize my data, this is why I'm asking. I'm considering using django-mptt (opposed to Models created by a loop at deployment time, each Model corresponding to a tree level), but I am scared that it would block me at a point and force me to review my whole project.

Was it helpful?

Solution

Found my answer finally : django-mptt dev documentation

Level The level (or “depth”) at which a node sits in the tree.

Root nodes are level 0, their immediate children are level 1, their immediate children are level 2 and so on...

This field is purely denormalisation for convenience. It avoids the need to examine the tree structure to determine the level of a particular node and makes queries which need to take depth into account easier to implement using Django’s ORM. For example, limiting the number of levels of nodes which are retrieved for the whole tree or any subtree:

# Retrieve root nodes and their immediate children only
SomeModel.tree.filter(level__lte=1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top