Django 2 to 3 upgrade throws error: "on_delete must be callable"

536 views Asked by At

In upgrading from Django 2 to 3.2, upon running my server, I suddenly get the following error:

TypeError: on_delete must be callable.

The model field in question is this one:

game_played = models.ForeignKey(Game, "Game Played", help_text="Game Played", null=True, blank=False)

As you can see, on_delete is not called on this field. Reverting back to Django 2 fixes this issue in that it no longer returns the error. I have tried to add on_delete to the field, which results in this error:

TypeError: __init__() got multiple values for argument 'on_delete'

I have reviewed the Django 3 docs and cannot find anything related to changes to on_delete that would affect this. All I can find regarding on_delete issues are that sometimes people put models.CASCADE in quotes versus making it callable but that is not the issue here.

How would I fix this issue?

2

There are 2 answers

0
Abdul Aziz Barkat On BEST ANSWER

You are supposed to provide the argument for on_delete one cannot forego it. Also the second argument for the __init__ of ForeignKey is not the verbose_name which you are assuming, instead it is on_delete, hence you should pass verbose_name as a kwarg if you need to:

# 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.

0
Chaibedraa Ibrahim On

Just add on_delete, and i don't think you need "Game Played", so it will look like this:

game_played = models.ForeignKey(Game, help_text="Game Played", null=True, blank=False, on_delete = models.CASCADE)

Then makemigrations and migrate in your terminal