leech
leech

Reputation: 8421

Django MPTT get related objects

I have a category tree and i'd like to get all products that are in the category tree. MPTT's documentation indicates that it only has methods that you can call to get objects.

I'm wondering how i can get it to work with related objects, for example, this syntax would ideal:

Product.objects.get(Q(category__ancestors=my_category)|Q(category=my_category))

Is there anything like this in django-mptt?

Upvotes: 2

Views: 1126

Answers (1)

jpic
jpic

Reputation: 33410

Try nesting the get_descendants() queryset in the product queryset:

Product.objects.get(category__in=my_category.get_descendants(include_self=True))

That should be the same than doing:

Product.objects.get(category__pk__in=my_category.get_descendants(include_self=True).values_list('pk'))

Upvotes: 2

Related Questions