Reputation: 1127
Let's say I have 100 players and 10 teams how can I remove any player from FroreignKey
drop-down chooser that is already was chosen for another team?
inside SpielerTeamRelationshipModel
I have player = models.ForeignKey(Spieler)
and I would like it not to show players who have already been selected for another teams. Is this possible?
class Spieler(models.Model):
name = models.CharField(max_length=128)
vorname = models.CharField(max_length=128)
panels = [
FieldPanel('name', classname="col6"),
FieldPanel('vorname', classname="col6"),
]
class SpielerTeamRelationshipModel(models.Model):
player = models.ForeignKey(Spieler)
in_team_seit = models.DateField()
page = ParentalKey('TeamRooster',
related_name='spieler_in_team')
panels = [
FieldPanel('player', classname="col6"),
FieldPanel('in_team_seit', classname="col6")
]
class TeamRooster(Page):
content_panels = [
FieldPanel('title'),
InlinePanel(
'spieler_in_team', label="Spieler",
panels=None)
]
One Player can be only in one Team. One Team can have one or more players. Very convenient for this is InlinePanel
, but every time to choose one player from 100, it's excruciating.
Or maybe I'm not doing the right thing and there's a smarter way to solve this problem?
Upvotes: 0
Views: 114
Reputation: 436
Strange but no one have mentioned unique_together. It will help you to create unique relations between team and player.
class Player(models.Model):
name = models.CharField(max_length=128)
team = models.ForeignKey(Team, blank=True, null=True)
class Meta:
...
unique_together = ("id", "team")
or sometimes you will need this
class Meta:
...
unique_together = ("name", "team")
Good luck!
Upvotes: 1
Reputation: 778
Instead of having a player as FK in team
You can solve the problem by having team as FK in Player table
Upvotes: 0