+
+ def validate_parents(self, parent=None):
+ if parent == None:
+ parent = self.parent
+
+ while parent:
+ try:
+ self.validate_parent(parent)
+ parent = parent.parent
+ except ObjectDoesNotExist:
+ return # because it likely means the child doesn't exist
+
+ def validate_parent(self, parent):
+ #Why doesn't this stop the Admin site from saving a model with itself as parent?
+ if self == parent:
+ raise ValidationError("A %s can't be its own parent." % self.__class__.__name__)
+
+ def clean(self):
+ super(TreeModel, self).clean()
+ self.validate_parents()
+
+ def save(self, *args, **kwargs):
+ self.clean()
+ super(TreeModel, self).save(*args, **kwargs)