2

I want to access the latest object a django-mptt tree.

Is it possible to do this from a django template?

Hobhouse
  • 15,463
  • 12
  • 35
  • 43

1 Answers1

4

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.

Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100