How do I get the last child in a django-mptt tree?
I want to access the last object of the django-mptt tree.
Is it possible to do this from a django template?
+2
Hobhouse
source
to share
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
Benjamin wohlwend
source
to share