Storing hierarchical (parent / child) data in Python / Django: MPTT alternative?

I am looking for a good way to store and use hierarchical (parent / child) data in Django. I used django-mptt , but it seems completely incompatible with my brain - as a result, I get non-obvious errors in non-obvious places, mainly when moving things in a tree: I end up an inconsistent state where the node and its parent will not agree to their relationship.

My needs are simple:

  • Given node:
    • find your root
    • find your ancestors
    • find your descendants
  • With a tree:
    • easy to move nodes (i.e. change parent)

My trees will be small (no more than 10 thousand nodes of more than 20 levels, as a rule, much smaller, say, 10 nodes with 1 or 2 levels).

I should think that there should be an easier way to make trees in python / django. Are there other approaches that better help maintain consistency?

+5
source share
1 answer

django-treebeard is another option. He has excellent documentation. I believe that it meets all your requirements and includes some functions for checking the tree for problems and eliminating these problems in the tree.

Node.find_problems() https://tabo.pe/projects/django-treebeard/docs/1.60/api.html#treebeard.models.Node.find_problems

Node.fix_tree() https://tabo.pe/projects/django-treebeard/docs/1.60/api.html#treebeard.models.Node.fix_tree

+3
source

All Articles