I have two foreign key fields that point to the same model. I would like to prevent them from having the same value. Here is my code that does not work
class Match(models.Model):
team_a = models.ForeignKey(Team, related_name='team_a', on_delete=models.CASCADE)
team_b = models.ForeignKey(Team, related_name='team_b', on_delete=models.CASCADE)
match_date_time = models.DateTimeField(validators=[validate_matchdate])
winner = models.CharField(choices=CHOICES, max_length=50, blank=True, null=True)
def clean(self):
direct = Match.objects.filter(team_a = self.team_a, team_b=self.team_b)
reverse = Match.objects.filter(team_a = self.team_b, team_b=self.team_a)
if direct.exists() & reverse.exists():
raise ValidationError('A team cannot be against itself')