Django-mptt order

前端 未结 2 622
说谎
说谎 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 17:19

    With recent mptt versions (e.g. 0.8.7) you should use the TreeForeignKey field:

    from mptt.models import MPTTModel
    from mptt.fields import TreeForeignkey
    
    class Category(MPTTModel):
        name = models.CharField()
        parent = TreeForeignKey("self", 
                               blank=True, 
                               null=True, 
                               related_name="sub_category")
    
        class MPTTMeta:
            order_insertion_by = ['name']
    

提交回复
热议问题