计算类型为None的字段的最终得分的方法

时间:2018-03-09 21:01:17

标签: django wagtail

请帮助我弄清楚如何正确检查字段的值并绘制目标总分。问题是最终估算是通过pre_save方法计算的。模特有上半场比赛,下半场比赛和枪战得分。用户可以更新第一个半场得分并保存该模型pre_save将获取此值,将其添加到总得分并忽略第二个半场和枪战。但是在比赛的下半场结束后,用户必须更新下半场的得分,然后pre_save方法将计算这两个值(第一和第二)并忽略枪战等等。我希望你能理解我将会取得的成就。这是我的模特:

class GroupstageTournamentModel(ClusterableModel):
    #Score of TEAM 1
    team_1_first_halftime_score = models.PositiveSmallIntegerField(blank=True, default=None, verbose_name='Resultat 1. HZ')
    team_1_first_halftime_point = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Punkte 1. HZ')
    team_1_second_halftime_score = models.PositiveSmallIntegerField(blank=True, default=None, verbose_name='Resultat 2. HZ')
    team_1_second_halftime_point = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Punkte 2. HZ')
    team_1_shootout_score = models.PositiveSmallIntegerField(blank=True, default=None, verbose_name='Resultat Shootout')
    team_1_shootout_point = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Schootout Punkte')
    team_1_total_score = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Resultat Total')
    team_1_total_points = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Punkte Total')

    #Score of TEAM 2
    team_2_first_halftime_score = models.PositiveSmallIntegerField(blank=True, default=None, verbose_name='Resultat 1. HZ')
    team_2_first_halftime_point = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Punkte 1. HZ')
    team_2_second_halftime_score = models.PositiveSmallIntegerField(blank=True, default=None, verbose_name='Resultat 2. HZ')
    team_2_second_halftime_point = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Punkte 2. HZ')
    team_2_shootout_score = models.PositiveSmallIntegerField(blank=True, default=None, verbose_name='Resultat Shootout')
    team_2_shootout_point = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Schootout Punkte')
    team_2_total_score = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Resultat Total')
    team_2_total_points = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Punkte Total')

    @receiver(pre_save, sender='tournament.GroupstageTournamentModel')
    def my_callback(sender, instance, *args, **kwargs):
        # Point for first half time
        if not (instance.team_1_first_halftime_score is None and instance.team_2_first_halftime_score is None):
            if instance.team_1_first_halftime_score > instance.team_2_first_halftime_score:
                instance.team_1_first_halftime_point += 2
        elif not (instance.team_1_first_halftime_score is None and instance.team_2_first_halftime_score is None):
            if instance.team_2_first_halftime_score > instance.team_1_first_halftime_score:
                instance.team_2_first_halftime_point += 2
        elif not (instance.team_1_first_halftime_score is None and instance.team_2_first_halftime_score is None):
            if instance.team_1_first_halftime_score == instance.team_2_first_halftime_score:
                instance.team_2_first_halftime_point += 1
                instance.team_1_first_halftime_point += 1
        # Point for second half time
        if not (instance.team_1_second_halftime_score is None and instance.team_2_second_halftime_score is None):
            if instance.team_1_second_halftime_score > instance.team_2_second_halftime_score:
                instance.team_1_second_halftime_point += 2
        elif not (instance.team_1_second_halftime_score is None and instance.team_2_second_halftime_score is None):
            if instance.team_2_second_halftime_score > instance.team_1_second_halftime_score:
                instance.team_2_second_halftime_point += 2
        elif not (instance.team_1_second_halftime_score is None and instance.team_2_second_halftime_score is None):
            if instance.team_1_second_halftime_score == instance.team_2_second_halftime_score:
                instance.team_2_second_halftime_point += 1
                instance.team_1_second_halftime_point += 1
        # Point for Shootout
        if not (instance.team_1_shootout_score is None and instance.team_2_shootout_score is None):
            if instance.team_1_shootout_score > instance.team_2_shootout_score:
                instance.team_1_shootout_point += 1
        elif not (instance.team_1_shootout_score is None and instance.team_2_shootout_score is None):
            if instance.team_2_shootout_score > instance.team_1_shootout_score:
                instance.team_2_shootout_point += 1
        elif not (instance.team_1_shootout_score is None and instance.team_2_shootout_score is None):
            if instance.team_1_shootout_score == instance.team_2_shootout_score:
                instance.team_1_shootout_point += 0
                instance.team_2_shootout_point += 0
        instance.team_1_total_score = instance.team_1_first_halftime_score + instance.team_1_second_halftime_score + instance.team_1_shootout_score
        instance.team_2_total_score = instance.team_2_first_halftime_score + instance.team_2_second_halftime_score + instance.team_2_shootout_score
        instance.team_1_total_points = instance.team_1_first_halftime_point + instance.team_1_second_halftime_point + instance.team_1_shootout_point
        instance.team_2_total_points = instance.team_2_first_halftime_point + instance.team_2_second_halftime_point + instance.team_2_shootout_point

我已经创建了一些第一,第二和枪战得分的计算。我最初想要削减这部分代码,但也许这会更清楚我将要做的事情。

请注意。如果我将默认值定义为0,则很容易做到,但在这种情况下,我的pre_save方法会自动为两个团队添加1点,因为匹配以0 0的分数结束。

当前代码返回TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' 我开始修复它,但我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我做了以下工作。但任何建设性的批评都是受欢迎的。

-5