在下面的屏幕截图中,您可以看到我试图保存此模型给出一个值为" RESULTAT 1 HZ"和#34; RESULTAT 1 HZ"的空值在下行。在我的my_callback
进行一些计算,但正如您只能看到两个字段都不是None
一样。那么,为什么我得到第二个屏幕截图上显示的错误?
TypeError
Exception Value: '>' not supported between instances of 'int' and 'NoneType'
@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
答案 0 :(得分:2)
if not (
instance.team_1_first_halftime_score is None and
instance.team_2_first_halftime_score is None
):
只有当以下两项都False
导致比较None
和int
类型时,此条件才为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: