在视图中:
context['categories'] = = models.Category.objects.all().get_cached_trees()在模板中:
{% load mptt_tags %}
<ul>
{% recursetree categories %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>因此,它只呈现第一级查询集。如果移除get_cached_trees,它将呈现所有树。如何用get_cached_trees渲染所有的树
发布于 2020-02-19 12:49:09
在这种情况下,您实际上不需要调用get_cached_trees(),因为递归树已经为您执行了缓存。
从文档中:
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)https://stackoverflow.com/questions/60288862
复制相似问题