I came in touch with a little problem while building a model with a foreign key to itself.
Here an example:
class Example (model.Model):
parent = models.ForeignKey('self', null=True, blank=True)
# and some other fields
After creating a new entry in the admin panel and going into this example for editing some content, I realize that I could set the parent to the current entry. But thats not what I wanted to get with the ForeignKey and the relation to itself.
Is it possible to disallow the link to itself?
Maybe it’s better to use a integer field with the right choices but I’m not quiet sure how to realize this an a smooth and Python like way.
one way to do this would be to override the model’s clean method
this will be called as the second step of object validation and will prevent the object from being inserted in the database.