1

を使用django-mpptして、現在のカテゴリに関連するオブジェクトの量をその子のいずれかに表示して、カテゴリ階層をブラウズしたいと考えています。

示されている例によく似drill_down_for_nodeていますが、現在のノードの子のみ...

最適なのは

{% recursetree obj.get_children cumulative count model.Foreignkey.category in o_count%}
<li>
    <h2><a href="{{ node.get_absolute_url }}">{{ node }}</a></h2>
    {% if not node.is_leaf_node %}
    <ul class="children">
        <a href="{{ children.get_absolute_url }}">{{ children }} ({{o_count}})</a>
    </ul>
    {% endif %}
</li>
{% endrecursetree %}

ポインタはありますか?

4

1 に答える 1

2

TreeManager でこの関数を見つけました:

https://github.com/django-mptt/django-mptt/blob/master/mptt/managers.py#L250

ビューのクエリセットにカウントを追加します。

context['qs_with_related_count'] = model1.objects.add_related_count(
                                          obj.get_children(),
                                          model2, 
                                          'category',
                                          'o_count',
                                          True
                                   )

そしてテンプレートで:

{% recursetree qs_with_related_count %}
    <li>
        <h2><a href="{{ node.get_absolute_url }}">{{ node }} ({{ node.o_count }})</a></h2>
        {% if not node.is_leaf_node %}
           <ul class="children">
               {{ children }}
           </ul>
        {% endif %}
    </li>
 {% endrecursetree %}

ManyToManyField残念ながら、私はasを使用しようとしていましたrel_field:

https://github.com/django-mptt/django-mptt/issues/90

しかし、あなたは運が良さそうです(「カテゴリ」ではなく「カテゴリ」):)

于 2013-01-21T23:13:27.350 に答える