[Answered ]-How to make two foreign keys to same model unique together?

2👍

I suggest you to use model.clean method:

class Friendship(models.Model):
    person1 = models.ForeignKey(Person, related_name='person1')
    person2 = models.ForeignKey(Person, related_name='person2')

    def clean(self):
        direct = FriendShip.objects.filter(person1 = self.person1, person2 = self.person2)
        reverse = FriendShip.objects.filter(person1 = self.person2, person2 = self.person1) 

        if direct.exists() or reverse.exists():
            raise ValidationError({'key':'Message')})
👤Serjik

Leave a comment