Let's say I have these Models:
class Category(MP_Node):
name = models.CharField(max_length=30)
class Item(models.Model):
category = models.ForeignKey(Category)
and I would like to find all Item
s belonging to any descendant of a given Category
.
Usually I would write category.item_set
but this is just Item
s belonging to the given level of the hierarchy.
Using the example tree in the treebeard tutorial, if an Item belongs to "Laptop Memory", how would I find all Items belonging to descendants "Computer Hardware" where "Laptop Memory" is one of those descendants?
For example, you can get all descendants of a category with get_descendants() in
views.py
as shown below. *You can see my answer explaining how to get all descendants of a category including the model instance itself withget_descendants(include_self=True)
: