2

I am trying to retrieve the root nodes of a hierarchy. My nodes look like this:

class MyNode(MPTTModel):
    parent = TreeForeignKey('self', blank=True, null=True, 
                            related_name='children')
    slug = models.SlugField(max_length=100, unique=True)
    title = models.CharField(max_length=100)
    user = models.ForeignKey(User)

and I call

MyNode.tree.filter(level=0)

to retrieve the root nodes as the documentation says here: http://django-mptt.github.io/django-mptt/technical_details.html#level

But when I execute that code, I get this error:

AttributeError: type object 'MyNode' has no attribute 'tree'

What object am I supposed to use to retrieve the root nodes then? Thank you!

pkout
  • 6,430
  • 2
  • 45
  • 55

1 Answers1

3

I've been in the same place as you, and I solve that by doing

MyNode.objects.filter(level=0) # or level=1 or level__lte=1...

I've read same docs as you and I tried to do like in the example and never make the code running. I hope this solution is enough for you!

AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
  • Thanks Liarez! I will give it a shot tonight and see if it works. It probably will. Perhaps this is not a bug in the code, but bug in the documentation instead. – pkout May 29 '14 at 14:29
  • 1
    You're right, it's a bug in the docs. I've just pushed the fix. – craigds May 29 '14 at 20:26
  • Looks like we collectively fixed the bug. Great! – pkout May 30 '14 at 03:08