1

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'

Johnny
  • 59
  • 2
  • 7

2 Answers2

2

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')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

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

Giannis
  • 5,286
  • 15
  • 58
  • 113
  • Have you created / run migrations? I can't provide much help as I am not familiar with the library, but `isinstance(Platform.objects.first(), MPTTModel)` should be True – Giannis Mar 24 '18 at 11:11