Home > Enterprise >  How to fix: missing 1 required positional argument: 'on_delete'
How to fix: missing 1 required positional argument: 'on_delete'

Time:11-26

When I was working on a Django project (blog), I had an error(s) while working on the site. Here are the errors I have appeared: 1: When I entered the python command manage.py makemigrations blog(via the console) in the directory C:\mysite\site\miniproject , then there is this:

Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    main()
  File "manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    django.setup()
  File "C:\Program Files\Python36\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Program Files\Python36\lib\site-packages\django\apps\config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\mysite\site\miniproject\blog\models.py", line 5, in <module>
    class Post(models.Model):
  File "C:\mysite\site\miniproject\blog\models.py", line 12, in Post
    author = models.ForeignKey(User, related_name='blog_posts')
TypeError: __init__() missing 1 required positional argument: 'on_delete'

Although I did everything according to the instructions on the website https://pocoz .gitbooks.io/django-v-primerah/content/sozdanie-i-primenenie-migracij.html. I did everything according to plan, I did everything in order and there was such a mistake. And I do not know how to fix it

Updated all the necessary libraries, entered them in manage.ру (which is located in the directory C:\mysite\site\miniproject ) import django, it didn't help

CodePudding user response:

If you debug your error its pretty self-explanatory: In line 12 File "C:\mysite\site\miniproject\blog\models.py", line 12, in Post in your Post model you have a field

author = models.ForeignKey(User, related_name='blog_posts')

You need to change that to:

author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')

CodePudding user response:

You have declared a ForeignKey somewhere but not provided the on_delete keyword argument.

If you post the BlogPost model, I can give you an exact answer, but you probably want something like:

models.ForeignKey(..., on_delete=models.CASCADE)

To fix the issue add the key word argument to the BlogPost model in blog/models.py

  • Related