I have the following models
from django.db import models
class league(models.Model):
name = models.CharField(max_length=64)
def __str__(self):
return f"{self.name}"
class player(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=128)
age = models.IntegerField()
position = models.CharField(max_length=1)
nteam = models.CharField(max_length=128)
def __str__(self):
return f"{self.id} {self.name} {self.age} {self.position} {self.nteam}"
class team(models.Model):
name = models.CharField(max_length=64)
league = models.ForeignKey(league, null=True, blank=True, on_delete=models.CASCADE, related_name="teams")
roster = models.ManyToManyField(player, blank=True, related_name="rosters")
def __str__(self):
return f"{self.name} {self.league}"
My goal would be to have many different leagues, each league containing multiple teams. I want a player only to be added to one team in a league. Right now I created a league, add several teams, then added players to each team. As of now I can take a player and add to all the teams in the same league if I want. How would you add a constraint so that once a player has been added to a team, they can no longer be added to another team in the same league?