djangodjango-treebeard

How do I filter for leaf nodes in an MP_NODE


I have a tree model for categories like this:

from treebeard.mp_tree import MP_Node

class Category(MP_Node):
    ...

And I want to get a queryset with only the leaf nodes.


Solution

  • MP_Node has a field called numchild, which stores the amount of children each node has, so you can get a queryset of all leaf nodes like this:

    Category.objects.filter(numchild=0)