Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve TypeError: on_delete must be callable on Django models?

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 must be callable.

like image 731
GTA.sprx Avatar asked Dec 10 '19 17:12

GTA.sprx


1 Answers

A field in one of your models has:

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

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')

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

like image 73
Willem Van Onsem Avatar answered Nov 18 '22 09:11

Willem Van Onsem