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
|-subcat11 0
|-sub11a 0
|-sub11b 1
\-sub11c 2
\-subcat12 1
CatB 1
|-subcat21 0
|-subcat22 1
\-subcat23 2
\-sub23a 0
CatC 2
How can I quickly fill/recalculate order column on creating/moving elements.
Or calculate it by Category's method Category.objects.get(name='sub11b').get_order()
should return 1.