简体   繁体   English

模板中带有 get_cached_trees 的 Django mptt recursetree

[英]Django mptt recursetree with get_cached_trees in template

In View:在视图中:

context['categories'] =  = models.Category.objects.all().get_cached_trees()

In template:在模板中:

{% load mptt_tags %}
<ul>
    {% recursetree categories %}
        <li>
            {{ node.name }}
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

As a result, it renders only first level of queryset.因此,它仅呈现第一级查询集。 If remove get_cached_trees it renders all tree.如果删除get_cached_trees它将呈现所有树。 How to render all tree with get_cached_trees ?如何使用get_cached_trees渲染所有树?

You don't actually need to call get_cached_trees() in this situation because recursetree already does the caching for you.在这种情况下您实际上不需要调用 get_cached_trees() 因为 recursetree 已经为您做了缓存。

From the documentation:从文档:

Iterates over the nodes in the tree, and renders the contained block for each node.
This tag will recursively render children into the template variable {{ children }}.
Only one database query is required (children are cached for the whole tree)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM