class Category(models.Model):
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
title = models.CharField(max_length=50)
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)