简体   繁体   中英

django-mptt model instance does not support indexing

I'm trying to implement a simple file browser app using django-mptt

this is my 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

  {% 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

'RootMPTT' object does not support indexing

mainly because the 'nodes' variable is the below

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

if i change it to

nodes = RootMPTT.objects.all()

the html is rendered fine. 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. but would like to know if theres a method using recursetree

recursetree takes a queryset or list of nodes, not a single node. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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