How to Solve Error Message TypeError: __init__() missing 1 required positional argument: ‘on_delete’ when executing python manage.py makemigrations

Posted on

This article will focus on the discussion of how to solve the error message as in the title of this article. Actually, the error message is quite clear. It is pointing out a missing argument. An action of executing the command ‘python manage makemigrations’ trigger the error message. It is when there is a need to migrate or to implement model definition in the ‘models.py’ file. The command will transform the model definition into the associated table in the database. The error message itself exist in a complete output as follow :

user@hostname:~/python/django/todoproject$ source /home/user/python/django/env/bin/activate
(env) user@hostname:~/python/django/todoproject$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in 
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/user/python/django/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1006, in _gcd_import
  File "", line 983, in _find_and_load
  File "", line 967, in _find_and_load_unlocked
  File "", line 677, in _load_unlocked
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "/home/user/python/django/todoproject/todoapp/models.py", line 17, in 
    class TodoList(models.Model): #Todolist able name that inherits models.Model
  File "/home/user/python/django/todoproject/todoapp/models.py", line 22, in TodoList
    category = models.ForeignKey(Category, default="general") # a foreignkey
TypeError: __init__() missing 1 required positional argument: 'on_delete'
(env) user@hostname:~/python/django/todoproject$ 

The error itself happens because of the model definition exist in the ‘models.py’ file. There is a content according to the output message above hinting the error part. That part is ‘ category = models.ForeignKey(Category, default=”general”) ‘. Furthermore, that part of the source code specific in that lines is missing 1 required position argument : ‘on_delete’. Actually, according to the above error output message, the solution is simple. Just add that 1 required positional argument. That is the ‘on_delete’ argument according to the same error output message. So, just add the additional argument to the part of the line definiton as follows :

    category = models.ForeignKey(Category, default="general",on_delete=models.CASCADE) # a foreignkey

Soon after, just re-execute the ‘python manage.py makemigrations’. If everything runs well, the following is the actual output of the command re-execution as follows :

(env) user@hostname:~/python/django/todoproject$ python manage.py makemigrations
Migrations for 'todoapp':
  todoapp/migrations/0001_initial.py
    - Create model Category
    - Create model TodoList
(env) user@hostname:~/python/django/todoproject$ 

Fortunately, it works. Just check the database to find out if the table of ‘Category’ and ‘TodoList’ exist to represent each of the model. The source code is part of the lesson or article exist in the article with the title of ‘How to Build Todoapp with Django’ in this link.

Leave a Reply