B
B
bbquite2021-08-09 12:36:44
Django
bbquite, 2021-08-09 12:36:44

How to display only the first two children in django-mptt?

There is this pattern:

{% recursetree product.comments.all %}
               {% with comment=node %}
                    <div class="comment-item_lvl-{{ comment.level|add:'1' }}">
                        {% include 'product/comment.html' %}
                    </div>
                {% endwith %}
                {% if not node.is_leaf_node %}
                      {{ children }}
                {% endif %}
 {% endrecursetree %}


How to set the output of only the first 2 descendants and not all?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-08-09
@WebDev921

Level empirically determine yourself

{% recursetree product.comments.all %}
    {% with comment=node %}
    <div class="comment-item_lvl-{{ comment.level|add:'1' }}">
        {% include 'product/comment.html' %}
    </div>
    {% endwith %}
    {% if not node.is_leaf_node and node.level <= 2 %}
        {{ children }}
    {% endif %}
{% endrecursetree %}

And in pursuit, it's better
{% recursetree product.comments.all %}
    <div class="comment-item_lvl-{{ node.level|add:'1' }}">
        {% include 'product/comment.html' with comment=node %}
    </div>
    {% if not node.is_leaf_node and node.level <= 2 %}
        {{ children }}
    {% endif %}
{% endrecursetree %}

A
alternativshik, 2021-08-09
@alternativshik

{% recursetree product.comments.all | slice:":2" %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question