delete必须在Django模型上可调用

delete必须在Django模型上可调用

本文介绍了如何解决TypeError:on_delete必须在Django模型上可调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突然我收到一条错误消息,提示 TypeError:on_delete必须是可调用的.我不知道如何解决此错误,因为我没有看到 field = models.ForeignKey(default= 1,on_delete ='CASCADE',to ='main.Category'),在我的代码中的任何地方都提到过.

Suddenly I am getting an error saying TypeError: on_delete must be callable. I don't know how to solve this error as I don't see field=models.ForeignKey(default=1, on_delete='CASCADE', to='main.Category'), mentioned anywhere in my code.

  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必须是可调用的.

TypeError: on_delete must be callable.

推荐答案

其中一个模型中的字段具有:

A field in one of your models has:

class SomeModel(models.Model):
    # …
    field=models.ForeignKey(default=1, , to='main.Category')

但是您不能将字符串传递给 on_delete =… 参数,它应该是:

but you can not pass a string to on_delete=… parameter, it should be:

class SomeModel(models.Model):
    # …
    field=models.ForeignKey(default=1, on_delete=models.CASCADE, to='main.Category')

您还需要删除迁移文件( main/migrations/0014_auto_20191025_1154.py ),然后进行新的迁移.

You will need to remove the migration file (main/migrations/0014_auto_20191025_1154.py) as well, and make new migrations.

这篇关于如何解决TypeError:on_delete必须在Django模型上可调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:58