[Django]-Django-mptt filter child from django template

3👍

level property and get_previous_sibling can be used to achieve this:

{% load mptt_tags %}

{% recursetree nodes %}

  {% if node.level == 0 %}
    {# first level #}
    {{ node.name }}
  {% elif not node.get_previous_sibling %}
    {# first child #}
    {{ node.name }}
  {% endif %}

  {{ children}}
{% endrecursetree %}

Please note that get_previous_sibling call will trigger db query.

Leave a comment