突然我收到一条错误消息,说TypeError: on_delete must be callable.
我不知道如何解决此错误,因为我在代码的任何地方都没有看到field=models.ForeignKey(default=1, on_delete='CASCADE', to='main.Category'),
。
File "/home/arch/myproject/main/migrations/0014_auto_20191025_1154.py", line 6, in <module>
class Migration(migrations.Migration):
File "/home/arch/myproject/main/migrations/0014_auto_20191025_1154.py", line 47, in Migration
field=models.ForeignKey(default=1, on_delete='CASCADE', to='main.Category'),
File "/home/arch/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 801, in __init__
raise TypeError('on_delete must be callable.')
TypeError:on_delete必须是可调用的。
答案 0 :(得分:2)
其中一个模型中的一个字段具有:
class SomeModel(models.Model):
# …
field=models.ForeignKey(default=1, on_delete='CASCADE', to='main.Category')
但是您不能将字符串传递给on_delete=…
参数,它应该是:
class SomeModel(models.Model):
# …
field=models.ForeignKey(default=1, on_delete=models.CASCADE, to='main.Category')
您还需要删除迁移文件(main/migrations/0014_auto_20191025_1154.py
),然后进行新的迁移。
答案 1 :(得分:1)
根据django docs,on_delete
参数必须是可调用的,而不是字符串。
这是CASCADE
中定义的django.db.models
可调用项:
def CASCADE(collector, field, sub_objs, using):
collector.collect(sub_objs, source=field.remote_field.model,
source_attr=field.name, nullable=field.null)
if field.null and not connections[using].features.can_defer_constraint_checks:
collector.add_field_update(field, None, sub_objs)