django-mptt 未正确地将数据链接在一起

发布于 2024-10-07 03:04:41 字数 1119 浏览 0 评论 0原文

嘿,我正在使用 MPTT 从模型创建一些树状数据,包含对话,我希望它们按“投票”字段排序。

该模型目前看起来像这样,非常基本。

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']

正如您所看到的,我们有一个消息字段、链接到线程模型的父 FK 和一个投票。

在我的视图中,我

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

在我的模板中

{% 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>

有这个但是,输出的列表是所有线程的列表。它们都没有联系在一起。

有什么想法吗?

Hay, I'm using MPTT to create some tree-like data from a model which contains conversations, and i want them to be ordered by a 'votes' field.

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

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 parent FK which is linked to the Thread model, and a votes.

Within my views i have this

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

then within 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 outputted list is a a list of all the threads. None of them are linked together.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-10-14 03:04:41
{% 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>

在我的视图中:

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

在 html 页面中,我看到一棵按字母顺序排列并带有缩进的节点树。

{% 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 views I have:

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

In the html page I see a tree of nodes order alphabetically and with indentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文