How do I get the last child in a django-mptt tree?
1 answer
In python code, you can use get_children method . This should work:
children = node.get_children()
if children:
last_child = list(children)[-1]
To use this in a template, you need to write a simple template tag:
from django import template
register = template.Library()
@register.simple_tag
def last_child(node):
children = node.get_children()
if children:
return list(children)[-1]
else:
return ""
Check out the Django documentation for how to integrate this tag into your project.
+4
source to share