In my project, I use 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 be able to get the order for the current category as follows:
Category Navigation order(one column) CatA 0 |-subcat11 0 |-sub11a 0 |-sub11b 1 \-sub11c 2 \-subcat12 1 CatB 1 |-subcat21 0 |-subcat22 1 \-subcat23 2 \-sub23a 0 CatC 2
How to quickly fill / recount an order column when creating / moving items. Or compute it using the Category Category.objects.get(name='sub11b').get_order() method Category.objects.get(name='sub11b').get_order() should return 1.
python django django-mptt
Eugene nagorny
source share