How do I prevent a ManyToManyField('self') from linked an object to itself?
- by dyve
Consider this model (simplified for this question):
class SecretAgentName(models.Model):
name = models.CharField(max_length=100)
aliases = ManyToManyField('self')
I have three names, "James Bond", "007" and "Jason Bourne". "James Bond" and "007" are aliases of each other.
This works exactly like I want it to, except for the fact that every instance can also be an alias of itself. This I want to prevent. So, there can be many SecretAgentNames, all can be aliases of each other as long as "James Bond" does not show up as an alias for "James Bond".
Can I prevent this in the model definition? If not, can I prevent it anywhere else, preferably so that the Django Admin understands it?