简体   繁体   English

django-mptt模型实例不支持索引

[英]django-mptt model instance does not support indexing

I'm trying to implement a simple file browser app using django-mptt 我正在尝试使用django-mptt实现一个简单的文件浏览器应用程序

this is my models.py 这是我的models.py

class RootMPTT(MPTTModel):
    name = models.CharField(max_length =255)
    parent = TreeForeignKey('self',null=True,blank=True,related_name='children',db_index=True)

class Doc(models.Model):
    file = models.FileField(upload_to=set_upload_path_MPTT)
    belongs_to = models.ForeignKey(RootMPTT)

I'm trying to show the tree view in a html using the code from the tutorial section 我正在尝试使用教程部分中的代码以html形式显示树视图

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

I'm getting the below error from django 我从Django得到以下错误

'RootMPTT' object does not support indexing

mainly because the 'nodes' variable is the below 主要是因为'nodes'变量是下面的

nodes = RootMPTT.objects.get(pk=casenum)

if i change it to 如果我将其更改为

nodes = RootMPTT.objects.all()

the html is rendered fine. html呈现良好。 but all I need is to get the descendants of a single node as opposed to all root nodes. 但是我所要做的就是获取单个节点的后代,而不是所有根节点。

i supposed i can get the children by getting the get_children method and manually show them in the html. get_children我可以通过获取get_children方法来获取孩子,并在html中手动显示他们。 but would like to know if theres a method using recursetree 但想知道是否有使用递归树的方法

recursetree takes a queryset or list of nodes, not a single node. recursetree采用查询集或节点列表,而不是单个节点。 If you only want to show one tree, just make a queryset with only that tree in it: 如果您只想显示一棵树,则只需在其中设置一棵树的查询集即可:

nodes = RootMPTT.objects.get(pk=casenum).get_descendants(include_self=True)

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

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