如果条件不符合我的预期
If condition works not so as I expect it
在下面的屏幕截图中,您可以看到我正在尝试保存此模型,给 "RESULTAT 1 HZ" 一个值,而 "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
if not (
instance.team_1_first_halftime_score is None and
instance.team_2_first_halftime_score is None
):
仅当以下两项均为 None
时,此条件才为 False
,这将导致 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:
在下面的屏幕截图中,您可以看到我正在尝试保存此模型,给 "RESULTAT 1 HZ" 一个值,而 "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
if not (
instance.team_1_first_halftime_score is None and
instance.team_2_first_halftime_score is None
):
仅当以下两项均为 None
时,此条件才为 False
,这将导致 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: