有条件地设置比赛的回合数(OOP,Python)

时间:2018-08-28 09:01:46

标签: python django oop

我问这个问题的部分原因是因为我不知道问这个问题的正确方法是什么。我正在尝试为Django中的瑞士式锦标赛创建一个计算器。比赛可能具有不同的回合数,因此我需要能够获取每个回合的比分。这是我为锦标赛的单个参与者设计的模型:

class Speaker(models.Model):
    speaker = models.CharField(max_length = 32)
    team_name = models.ForeignKey(Team, on_delete = models.CASCADE)
    tournaName = models.CharField(max_length = 32) 
    round1 = models.IntegerField()
    round2 = models.IntegerField()
    round3 = models.IntegerField()
    round4 = models.IntegerField() #what if I only wanted three rounds? or five?
    totalScores = models.IntegerField()

    def updateTotals(self):
        self.totalScores = self.round1 + self.round2 + self.round3 + self.round4

基本上,我希望能够根据需要创建尽可能多的“舍入”变量。有什么方法可以做我想做的事情,还是应该重新考虑如何构造模型?

1 个答案:

答案 0 :(得分:0)

这是解决问题的一种方法

创建一个RoundScore模型,在其中存储每个回合,球员和锦标赛的得分。

class RoundScore:
    player = ForeignKey(Player) # ref to the player
    tournament = ForeignKey(Tournament) # ref to the tournament
    round = Integer() # the round number
    score = Integer() # the score for this round

然后有一个球员和锦标赛的模型,您可以在其中存储有关它们的信息。

使用这种解决方案,您可以在每个锦标赛中进行任意数量的回合,而不会出现问题,并且可以按回合方式为每个玩家获取分数,并将一个竞赛中一个玩家的所有分数相加。