迁移Entry Model和TypeError:__ init __()缺少1个必需的位置参数:'on_delete'

时间:2018-03-07 00:17:30

标签: django python-3.x

基本上在设置Django时,我正在尝试迁移条目模型,但在输入python manage.py makemigrations learning_logs

时,我一直在使用ll_env活动的虚拟环境时出错
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/james/Desktop/The_Universe/CS/Python/Practice_PCC/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/james/Desktop/The_Universe/CS/Python/Practice_PCC/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/Users/james/Desktop/The_Universe/CS/Python/Practice_PCC/learning_log/ll_env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/james/Desktop/The_Universe/CS/Python/Practice_PCC/learning_log/ll_env/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/Users/james/Desktop/The_Universe/CS/Python/Practice_PCC/learning_log/ll_env/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/james/Desktop/The_Universe/CS/Python/Practice_PCC/learning_log/learning_logs/models.py", line 12, in <module>
    class Entry(models.Model):
  File "/Users/james/Desktop/The_Universe/CS/Python/Practice_PCC/learning_log/learning_logs/models.py", line 14, in Entry
    topic = models.ForeignKey(Topic)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

这本书应该如下所示。

(ll_env)learning_log$ python manage.py makemigrations learning_logs
Migrations for 'learning_logs':
  0002_entry.py:
    - Create model Entry
(ll_env)learning_log$ python manage.py migrate
Operations to perform:
  --snip--
  Applying learning_logs.0002_entry... OK

所以基本上错误,正如TypeError所示,我错过了'on_delete'所需的1个位置参数吧?所以我试图找到'on_delete'部分,但我不知道这是用什么脚本写的。

TypeError究竟是什么意思?

1 个答案:

答案 0 :(得分:0)

看起来你在使用Django 2.0,如果是这种情况on_delete现在是ForeignKey的必需参数。如果你继续添加,我相信你不会看到那个错误。

https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey.on_delete