1

我不知道怎么问这个问题?首先让我解释一下我有TeamPlayer模型。

class team()
        name = models.CharField(max_length=100) // name quato something like this

class player(models.Model) 
        point = models.IntegerField()
        assist = models.IntegerField() 
        rebound = models.IntegerField() //something like this 
        team = models.ForeignKey(team)

我需要Match那场比赛的模特和球员(得分、助攻、篮板);如何创建模型来表示它?

4

2 回答 2

2

这是一个简化假设的练习/作业,还是您在为现实世界的应用程序建模?例如,随着时间的推移,球员可能会更换球队:你将如何建模?为简单起见,让我们假设玩家不会更换球队。我会为您的案例建模如下:

class Team(models.Model):
    name = models.CharField(max_length=100)
    # other attributes that do not change over time

class Player(models.Model):
    name = models.CharField(max_length=100)
    team = models.ForeignKey(Team)
    # other attributes that do not change over time

class Match(models.Model):
    venue = models.CharField(max_length=100)
    date = models.DateField()
    # other non-redundant attributes

class PlayerMatch(models.Model):
    player = models.ForeignKey(Player)
    match = models.ForeignKey(Match)

    points = models.IntegerField()
    assists = models.IntegerField()
    rebounds = models.IntegerField()

这里的重要部分是PlayerMatch仅保留统计信息。您不必重复它们 inPlayer或 in Match,那将是多余的。相反,可以在给定这些数据结构的情况下计算比赛或球员的统计数据。

于 2013-03-05T12:49:34.550 回答
1

我会做:

class StatisticsBase(models.Model):
    class Meta:
        abstract = True

    points = models.IntegerField()   # plural form reads easier here
    assists = models.IntegerField()  # at least in English
    rebounds = models.IntegerField()


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


class Match(StatisticsBase):
    players = models.ManyToManyField(Player)  # you should capitalize class names
    # any additional fields
于 2013-03-05T12:22:46.997 回答