I want to access the latest object a django-mptt tree.
Is it possible to do this from a django template?
I want to access the latest object a django-mptt tree.
Is it possible to do this from a django template?
In python code, you can use the get_children method. This should work:
children = node.get_children()
if children:
last_child = list(children)[-1]
To use this in a template, you'd 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 ""
Have a look at the Django documentation to find out how to integrate this tag into your project.