Search code examples
djangodjango-mptt

I'm looking for working example of django mptt


Does anyone has a working example of django mptt. I am not able to implement it. Maybe category system. Thanks


Solution

  • Here is my code, part of an accounting app:

    from mptt.models import MPTTModel
    from mptt.fields import TreeForeignKey
    
    class Category(MPTTModel):
        ledger = models.ForeignKey(Ledger)
        parent = TreeForeignKey(
            'self', null = True, blank = True, default = None,
            related_name = 'children'
        )
        label = models.CharField(max_length = 60)
    

    This simply follows the documentation. Which specific part goes wrong for you?