Django-mptt order

前端 未结 2 621
说谎
说谎 2021-02-04 17:08

In my project I am using django-mptt for categories.

My model:

class Category(models.model):
    name = models.CharField()
    parent = models.ForeignKey         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 17:16

    When defining the model you can specify the ordering with "order_insertion_by".

    So something like this:

    class Category(MPTTModel):
        name = models.CharField()
        parent = models.ForeignKey("self", blank=True, null=True, 
                 related_name="sub_category")
    
        class MPTTMeta:
            order_insertion_by = ['name']
    

    Then you can rebuild your database with Category.tree.rebuild() which should respect the ordering specified.

提交回复
热议问题