Johnny
Johnny

Reputation: 69

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'

Upvotes: 1

Views: 2780

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599956

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')

Upvotes: 2

Giannis
Giannis

Reputation: 5526

Your error says that you are either trying to access get_family on the wrong thing, or that you have not implemented the library correctly. Just having a glance at http://django-mptt.readthedocs.io/en/latest/models.html?highlight=get_family#get-family, you can see that you need to extend MPTTModel to have that function available

Upvotes: 0

Related Questions