Preventing certain values adding to Django model
I'm trying to put a restriction on my Django model. I have a user model and then a model which allows one user to "follow" another. I want to put a restriction so the user cannot follow themselves. It's not really working at all but I'm sort of stuck what to do. When I try to add a new object. It will let me alter an existing entry but if I try to create a new one I get the error:
Follower: Follower object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.
It's like I have to save it somehow before I check for the items but I'm not sure how to go about it.
Models
class User(AbstractUser):
pass
class Follower(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE, related_name="followers", unique=True)
follows = models.ManyToManyField(User,blank=True,related_name="follows")
# Prevent User from following themselves
def save(self, *args, **kwargs):
if self.follows.contains(self.user):
return
else:
super().save(*args, **kwargs)
I've tried variations of the if statement and I've tried using a for loop to go through the ManytoMany field but I can see the object doesn't exist yet