django-MPTT 钻取模板

django-MPTT drilldown template

我在获取模板中的 mptt 下钻时遇到一些问题。

我有以下型号。

models.py

class Dimension_value(MPTTModel):
    name = models.CharField(max_length = 200, null=True, blank = True, default = '')
    parent = TreeForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, related_name="children")

    class MPTTMeta:
        order_insertion_by = ['name']

def __str__(self):
    return self.name

views.py

def show_dimensions(request):
    return render(request, "accounts/dimension_detail.html", {'dimensions': Dimension_value.objects.all()})

template.py

{% extends 'base.html' %}
{% block head %}
{% endblock %}
{%block body%}
<div class="container-fluid">
   <div class="card">
      <div class="card-body">
         <h2>My dimensions</h2>
         {% load mptt_tags %}
         {% drilldown_tree_for_node dimensions as drilldown cumulative count accounts.Dimension_value.name in game_count %}
   {% for node,structure in drilldown|tree_info %}
       {% if structure.new_level %}<ul><li>{% else %}</li><li>{% endif %}
       {% if node == dimension %}
           <strong>{{ node.name }}</strong>
       {% else %}
           <a href="{{ node.get_absolute_url }}">{{ node.name }}</a>
           {% if node.parent_id == dimension.pk %}({{ node.game_count }}){% endif %}
       {% endif %}
       {% for level in structure.closed_levels %}</li></ul>{% endfor %}
   {% endfor %}
      </div>
   </div>
   <br>
   <br>
   <a class="btn btn-primary" href="{% url 'add_plan' %}" role="button">Add a plan</a>
   <button onclick="goBack()" class = "btn btn-secondary">Go Back</button><br><br><a class="btn btn-primary" href="{% url 'subscribeusertoplan' %}" role="button">Add a user to plan</a>
</div>
{%endblock%}

我已根据此处找到的文档添加了视图:https://django-mptt.readthedocs.io/en/latest/templates.html?highlight=examples#examples

但是我收到以下错误。 异常类型:属性错误 异常值:
'TreeQuerySet' 对象没有属性“_tree_manager”

希望有人能在正确的方向推动我。谢谢!

您可以使用一些 css 和 jquery

来做到这一点
{% block head %}
{% endblock %}
{%block body%}

<div class="container-fluid">

   <div class="card">
      <div class="card-body">
         <h2>My dimensions</h2>
         {% load mptt_tags %}

     {% recursetree dimensions %}
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse{{node.level}}" aria-expanded="false" aria-controls="collapsed">

             {{ node.name }}</a>
             {% if not node.is_leaf_node %}

                 <ul class="children">
                   <div id="collapse{{node.level}}" class="children panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
                     <div class="panel-body">

                     {{ children }}  </div>
                     </div>
                 </ul>
             {% endif %}

     {% endrecursetree %}

      </div>
   </div>
   <br>
   <a class="btn btn-primary" href="{% url 'add_plan' %}" role="button">Add a plan</a>
   <button onclick="goBack()" class = "btn btn-secondary">Go Back</button><br><br><a class="btn btn-primary" href="{% url 'subscribeusertoplan' %}" role="button">Add a user to plan</a>
</div>



{%endblock%}