Reputation: 6606
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
Upvotes: 1
Views: 695
Reputation: 2157
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)
Upvotes: 1