如果条件不符合我的预期

时间:2018-03-10 22:16:14

标签: django wagtail

在下面的屏幕截图中,您可以看到我试图保存此模型给出一个值为" RESULTAT 1 HZ"和#34; RESULTAT 1 HZ"的空值在下行。在我的my_callback进行一些计算,但正如您只能看到两个字段都不是None一样。那么,为什么我得到第二个屏幕截图上显示的错误? TypeError Exception Value: '>' not supported between instances of 'int' and 'NoneType'

enter image description here

@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

enter image description here

这是我的追溯http://dpaste.com/0DDP4QC

1 个答案:

答案 0 :(得分:2)

if not (
   instance.team_1_first_halftime_score is None and 
   instance.team_2_first_halftime_score is None
):

只有当以下两项都False导致比较Noneint类型时,此条件才为None,这会导致错误,如您所述上述

你可能想要跟随

if not (
   instance.team_1_first_halftime_score is None or
   instance.team_2_first_halftime_score is None
):

或附加条件

if instance.team_1_first_halftime_score and not instance.team_2_first_halftime_score:

这意味着你还需要

if not instance.team_1_first_halftime_score and instance.team_2_first_halftime_score: