How to limit choices for a Many to Many relation?

1k Views Asked by At

I'm using Python + Django and have this in my model right now:

class Team(models.Model):
    player = models.ManyToManyField(Player, related_name="player", through="Team_Player")
    squad = models.ManyToManyField(Player, related_name="squad", blank=True)

class Player(Person):
    name = models.CharField(max_length=100)

class Team_Player(models.Model):
    team = models.ForeignKey(Team)
    player = models.ForeignKey(Player)

Is there a way to limit Team.squad to only have players inside Team.player relationship? I was trying to use limit_choices_to with no luck so far. Any clues?

1

There are 1 best solutions below

0
On

There is no way to do this in the Django ORM. You will need to refactor your design so that a team is composed of squads, and a squad is composed of players; add a property or method to the team model to get all players in the current team.