2

ヘイ、私は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>

ただし、出力されるリストはすべてのスレッドのリストです。それらのどれも一緒にリンクされていません。

何か案は?

4

1 に答える 1

2
{% 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ページでは、ノードのツリーがアルファベット順にインデントされて表示されます。

于 2010-12-16T15:39:19.003 に答える