Django-mptt does not bind data together correctly

Hay, I use MPTT to create some tree-like data from a model containing conversations, and I want them to be sorted by the “votes” field.

The model looks like this at the moment, very simple.

class Thread(MPTTModel):
    message = models.CharField(max_length=100)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
    votes = models.IntegerField()

    class MPTTMeta:
        order_insertion_by=['votes']

As you can see, we have a message field and a parent FK, which is associated with the Thread model, and voices.

In my mind I have it

threads = Thread.tree.all()
    data = {
        'threads':threads
    }
    return render_to_response("show.html",data )

then in my template

{% load mptt_tags %}

<ul class="root">
        {% recursetree d %}
            <li>
                {{ node.title }}
                {% if not node.is_leaf_node %}
                    <ul class="children">
                        {{ children }}
                    </ul>
                {% endif %}
            </li>
        {% endrecursetree %}
</ul>

However, the list displayed is a list of all threads. None of them are interconnected.

Any ideas?

+3
source share
1 answer
{% load mptt_tags %}
<ul class="root">
    {% recursetree nodes %}            
        <li>
            {{ node.message }}
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

In the views I:

threads = Thread.tree.all()  
data = {  
    'nodes':threads  
}
return render_to_response("show.html",data )

On the html page, I see a tree of nodes in alphabetical order and indented.

+3

Source: https://habr.com/ru/post/1779868/


All Articles