Search code examples
pythondjangodjango-mptt

AttributeError: 'TreeQuerySet' object has no attribute 'get_family'


I am using django mptt and i want to get all whole family of one child. When i call other functions it works fine

For example i filter object and call function get_family

p = Platform.objects.filter(name__startswith='signals')
s = p.get_family()
print(s)

but getting error

AttributeError: 'TreeQuerySet' object has no attribute 'get_family'


Solution

  • get_family is a method on the model. But as the error shows, filter returns a QuerySet - ie a collection of models. You need to choose one to call your method on.

    Either use the .first() method:

    p = Platform.objects.filter(name__startswith='signals').first()
    

    or, if you're sure there is only ever one Platform object that matches, use get instead of filter:

    p = Platform.objects.get(name__startswith='signals')