由于您需要跟踪注册并将玩家分配到团队中,因此我看不到任何其他方式。实际上刮擦。
中间(GamePlayerMembership)实例的存在可以表明玩家是否已经注册了游戏。所以我的看法是这样的:
from django.db import models
class Player(models.Model):
name = models.CharField()
class Game(models.Model):
starts_when = models.DatetimeField()
players = models.ManyToManyField(Player, through='GamePlayerMembership', related_name='games_played')
def register(self, player):
return GamePlayerMembership.objects.create(game=self, player=player)
def assign(self, player, team):
try:
memb = GamePlayerMembership.objects.get(game=self, player=player)
memb.team = team
memb.save()
except GamePlayerMembership.DoesNotExist:
raise Exception('Player has not registered for the game')
class GamePlayerMembership(models.Model):
TEAMS = (
('home', 'Home'),
('guest', 'Guest')
)
game = models.ForeignKey(Game)
player = models.ForeignKey(Player)
team = models.CharField(max_length=5, choices=TEAMS, null=True, blank=True)
class Meta:
unique_together = ('game', 'player')