Answer the question
In order to leave comments, you need to log in
Django category tree how to limit nesting?
class Category(models.Model):
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
title = models.CharField(max_length=50)
Answer the question
In order to leave comments, you need to log in
First, use MPTT. Second, override the save() method and check the parent's level attribute in it.
class Category(mptt.models.MPTTModel):
parent = mptt.models.TreeForeignKey('self', null=True, blank=True, related_name='children')
title = models.CharField(max_length=50)
def save(self, *args, **kwargs):
if parent.level == 3:
raise ValueError(u'Достигнута максимальная вложенность!')
super(Category, self).save(*args, **kwargs)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question