Using django-mptt to get only the categories that have items -
sample structure:
all uppercase: category
mixed case: item
root ├── books │ ├── fiction │ │ └── classics │ └── non-fiction ├── clothing └── electronics ├── laptops ├── phones │ ├── apple │ │ ├── iphone 6 │ │ ├── iphone 6 plus │ │ ├── iphone 6s │ │ └── iphone 6s plus │ ├── motorola │ │ ├── moto g4 │ │ ├── moto g4 play │ │ ├── moto g4 plus │ │ └── moto x │ └── samsung └── tablets └── apple
i'm trying have index page show "electronics" category. "clothing" category shouldn't show since it's empty , "books" category shouldn't show either since, although has child categories, child categories don't have items.
similarly, "electronics" page should show "phones" category. "laptops" category shouldn't show since it's empty , "tablets" category shouldn't show either since, although has child category, child category doesn't have items.
similarly, "phones" page should show "apple" , "motorola" categories since "samsung" category empty.
what i've tried index page:
category.objects.root_nodes().exclude(children__isnull=true)
this excludes "clothing" category, not exclude "books" category. understand why doesn't work, don't know instead.
i don't think possible using django-mptt
manager methods , fields.
you come query filters categories items (think select distinct category_id items
) , find out set of root nodes of categories.
the bad news query won't perform use case described, maybe can afford having denormalized field (maybe subtree item count) or use search engine elasticsearch
kind of tasks.
Comments
Post a Comment