django-mptt

mptt with count of a different models instances that have tree model as foreignkey

孤人 提交于 2019-12-07 16:26:42
问题 In an online shop I use django-mptt for product categories with several levels. I also have products that each belong to a category. Now I'd like to visualise the category tree like this: all(18) - edible (10) -- food (4) -- drink (6) - inedible (8) -- wood (3) -- rock (5) where the numbers in scopes are the product counts for each category. I am able to visualize the category tree. I can also place the product count behind the category names, but the way I am doing it seems very inefficient.

How do I filter for all descendants of a ForeignKey MPTT Model?

泪湿孤枕 提交于 2019-12-06 12:49:28
Here is the situation. I'm utilizing an MPTT Model in Django to create a hierarchy of music genres (Rock, Hard Rock, etc). I'm assigning one of the nodes of this hierarchy to an Album. Let's say I create a Album object with Hard Rock genre. How can I query my Albums for all Rock albums and have it include Rock and all descendants of the Rock genre? class Genre(MPTTModel): name = models.CharField(max_length=50, unique=True) parent = TreeForeignKey('self', null=True, blank=True, related_name='children') class MPTTMeta: order_insertion_by = ['name'] def __unicode__(self): return self.name class

mptt with count of a different models instances that have tree model as foreignkey

為{幸葍}努か 提交于 2019-12-06 01:40:02
In an online shop I use django-mptt for product categories with several levels. I also have products that each belong to a category. Now I'd like to visualise the category tree like this: all(18) - edible (10) -- food (4) -- drink (6) - inedible (8) -- wood (3) -- rock (5) where the numbers in scopes are the product counts for each category. I am able to visualize the category tree. I can also place the product count behind the category names, but the way I am doing it seems very inefficient. I basically have a get_number_of_products() methods at the category model which returns the number of

Is it possible to integrate django-taggit and django-mptt / django-treebeard?

♀尐吖头ヾ 提交于 2019-12-05 14:18:00
I am developing a website that requires tagging up different types of content, which favors using django-taggit. But, it would be extremely beneficial if the tags were represented in the database in their natural hierarchy, favoring use of django-mptt or django-treebeard. What is the best solution to integrate the generic tagging functionality of taggit with the tree-structure provided by mptt / treebeard? Tentacles I used treebeard and taggit's custom through models to implement hierarchical tags. Just found this yesterday, so I can't really tell if it's what you're looking for: django

How would you sort a django mptt tree?

穿精又带淫゛_ 提交于 2019-12-05 03:43:15
问题 Imagine that I have an mptt tree of objects and their population like: Animal, 60 aardvark, 30 bobcat, 20 chipmunk, 10 Vegetable, 6 apple, 1 beet, 2 cauliflower, 3 Mineral 0 How would you sort the above by population on each sublevel? I want to get to: Animal, 60 aardvark, 30 bobcat, 20 chipmunk, 10 Vegetable, 6 cauliflower, 3 beet, 2 apple, 1 Mineral 0 I am building off of mptt in django. 回答1: I've just solved a similar issue. You can use an order_by , but not simply by the field you want to

show children nodes depending on selected parent

非 Y 不嫁゛ 提交于 2019-12-04 13:38:35
问题 Hi i've been looking all over and can't find the answer to this. I have only 3 months experience in using python/django so excuse my dummy quesion! Im using django mptt to display a simple nested set navigation. <ul class="root"> {% recursetree nodes %} <li> {{ node.name }} {% if not node.is_leaf_node %} <ul class="children"> {{ children }} </ul> {% endif %} </li> {% endrecursetree %} this works fine - however i would like to show only children of the selected category (based on slug) and not

How would you sort a django mptt tree?

落爺英雄遲暮 提交于 2019-12-03 22:16:01
Imagine that I have an mptt tree of objects and their population like: Animal, 60 aardvark, 30 bobcat, 20 chipmunk, 10 Vegetable, 6 apple, 1 beet, 2 cauliflower, 3 Mineral 0 How would you sort the above by population on each sublevel? I want to get to: Animal, 60 aardvark, 30 bobcat, 20 chipmunk, 10 Vegetable, 6 cauliflower, 3 beet, 2 apple, 1 Mineral 0 I am building off of mptt in django. affan I've just solved a similar issue. You can use an order_by , but not simply by the field you want to sort by: MyModel.tree.all().order_by('tree_id', 'level', 'your_sort_field') Try to add it in the

Django-MPTT full path to child pages how to make?

放肆的年华 提交于 2019-12-03 13:34:23
问题 I'm start using Django-MPTT app to get a tree-based approach on my Django-site pages. For ex. I have pages with sub pages: Trance: Vocal Trance(sub page) Hard Trance(sub page) Breaks: Atmo Breaks(sub page) Progressive Breaks(sub page) How can I get access to them from urls.py? What pattern will help? Do I need to store Full_path in model or it can be done via url pattern? 回答1: I assume you mean you want to do URLs like this: /trance/ /trance/vocal-trance/ /trance/hard-trace/ /breaks/ /breaks

efficient function to retrieve a queryset of ancestors of an mptt queryset

一曲冷凌霜 提交于 2019-12-03 11:27:59
Does anybody have an efficient algorithm to retrieve all ancestors of an mptt queryset? The best I could think of so far is something like this: def qs_ancestors(queryset): if isinstance(queryset, EmptyQuerySet): return queryset queryset_aggs = queryset.values_list('tree_id', 'level').annotate(max_lft=Max('lft'), min_rght=Min('rght')) new_queryset = queryset.none() for tree_id, level, max_lft, min_rght in queryset_aggs: ancestors = MyModel.objects.filter( tree_id=tree_id, level__lt=level, lft__lte=max_lft, rght__gte=min_rght, ) new_queryset = ancestors | new_queryset return new_queryset There

Django-mptt order

谁说我不能喝 提交于 2019-12-03 09:50:18
问题 In my project I am using django-mptt for categories. My model: class Category(models.model): name = models.CharField() parent = models.ForeignKey("self", blank=True, null=True, related_name="sub_category") nav_order = models.IntegerField(null=False, blank=False, default=0) # unsure need nav_order column in DB class Meta: verbose_name_plural = 'Categories' mptt.register(Category) And I need to have ability get order for current category like this: Category Navigation order(one column) CatA 0 |