2013-01-10 29 views
1

使用django-mppt我想瀏覽我的類別層次結構,顯示與任何孩子中的當前類別相關的對象數量。recoursetree的累計數量?

就像例所示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 %} 

任何指針?

+0

不遍歷'get_children'和他們中的每一個調用模板標籤幫幫我? –

+0

我想要一個與兒童相關的對象的累計數量,如「所有後代類別中的總數項目」... – tutuca

+0

您是否曾經爲此找到過解決方案? – sunn0

回答

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作爲rel_field

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

但看起來你很幸運(「類」而不是「類別」):)

+0

沒有時間來嘗試這個。但它可能是正確的。我會接受答案,因爲沒有新的答案。非常感謝你! – tutuca