Django模型,便于数据访问

时间:2016-01-30 15:23:14

标签: python django django-models

我正在努力建立健康的模型关系,使数据访问变得容易。

我有以下型号:

class Team(models.Model):
    name = models.CharField(max_length=200)

# A player has a name, plays for one team, and can have many roles in this team.
class Player(models.Model):
    name = models.CharField(max_length=200)
    team = models.ForeignKey(Team)
    role = models.ManyToManyField(Role)

# A role describes what a player does in a team. A role can be shared by multiple players and a player can have multiple roles.
class Role(models.Model):
    name = models.CharField(max_length=200)

# A match is between two teams, who have chosen a set of players from their total roster (usually not all of them), in certain roles.
class Match(models.Model):
    team_1 = models.ForeignKey(Team)
    team_2 = models.ForeignKey(Team)
    date_played = models.DateField()

Match类肯定是不完整的,因为我无法访问完整名单中的玩家子集,以及他们实际在该匹配中扮演的角色。我是否必须构建一个继承Team的Roster类?有什么想法吗?

我想要这样的事情:

> match = Match.objects.get(pk=1)
> match.team_1.players
[<Player 1>, <Player 2>, ...] # only starting players and substitutes that entered the match. 

相反,当然,我会得到完整的名单,包括他们可以扮演的所有角色,而不是他们实际玩过那场比赛的内容。

0 个答案:

没有答案