Django 2 to 3 upgrade throws error: "on_delete must be callable"
Django 2 to 3 upgrade throws error: "on_delete must be callable"
在从 Django 2 升级到 3.2 时,在 运行 我的服务器上,我突然收到以下错误:
TypeError: on_delete must be callable.
有问题的模型字段是这个:
game_played = models.ForeignKey(Game, "Game Played", help_text="Game Played", null=True, blank=False)
如您所见,on_delete
未在此字段上调用。恢复到 Django 2 修复了这个问题,因为它不再 returns 错误。我试图将 on_delete
添加到该字段,结果出现此错误:
TypeError: __init__() got multiple values for argument 'on_delete'
我查看了 Django 3 文档,但找不到任何与 on_delete
相关的更改会影响到此。关于 on_delete
问题,我所能找到的只是有时人们将 models.CASCADE
放在引号中而不是使其可调用,但这不是这里的问题。
我该如何解决这个问题?
你应该为 on_delete
提供一个不能放弃的论据。此外,ForeignKey
的 __init__
的第二个参数不是您假设的 verbose_name
,而是 on_delete
,因此您应该将 verbose_name
作为kwarg 如果你需要:
# pass `verbose_name` as kwarg
# pass `on_delete` it cannot be foregone
game_played = models.ForeignKey(Game, on_delete=models.CASCADE, verbose_name="Game Played", help_text="Game Played", null=True, blank=False)
Note: Don't know why you don't get this error in Django 2, looking at the source code the second argument is also on_delete
in that
version, should have given you some error at least at the time of
deletion if not at instantiation.
只需添加 on_delete,我认为您不需要“玩过游戏”,因此它看起来像这样:
game_played = models.ForeignKey(Game, help_text="Game Played", null=True, blank=False, on_delete = models.CASCADE)
然后在您的终端中进行迁移和迁移
在从 Django 2 升级到 3.2 时,在 运行 我的服务器上,我突然收到以下错误:
TypeError: on_delete must be callable.
有问题的模型字段是这个:
game_played = models.ForeignKey(Game, "Game Played", help_text="Game Played", null=True, blank=False)
如您所见,on_delete
未在此字段上调用。恢复到 Django 2 修复了这个问题,因为它不再 returns 错误。我试图将 on_delete
添加到该字段,结果出现此错误:
TypeError: __init__() got multiple values for argument 'on_delete'
我查看了 Django 3 文档,但找不到任何与 on_delete
相关的更改会影响到此。关于 on_delete
问题,我所能找到的只是有时人们将 models.CASCADE
放在引号中而不是使其可调用,但这不是这里的问题。
我该如何解决这个问题?
你应该为 on_delete
提供一个不能放弃的论据。此外,ForeignKey
的 __init__
的第二个参数不是您假设的 verbose_name
,而是 on_delete
,因此您应该将 verbose_name
作为kwarg 如果你需要:
# pass `verbose_name` as kwarg
# pass `on_delete` it cannot be foregone
game_played = models.ForeignKey(Game, on_delete=models.CASCADE, verbose_name="Game Played", help_text="Game Played", null=True, blank=False)
Note: Don't know why you don't get this error in Django 2, looking at the source code the second argument is also
on_delete
in that version, should have given you some error at least at the time of deletion if not at instantiation.
只需添加 on_delete,我认为您不需要“玩过游戏”,因此它看起来像这样:
game_played = models.ForeignKey(Game, help_text="Game Played", null=True, blank=False, on_delete = models.CASCADE)
然后在您的终端中进行迁移和迁移