在Django的models.py中创建数据库时,如何解决迁移错误?

时间:2019-01-14 07:07:04

标签: python django python-3.x django-models

我已经开始使用“无淀粉Python速成课程”来学习Django。在完成对“条目”进行编码的步骤时,我无法迁移数据库。 显示错误。我正在使用python 3.7和Django 2.0.7

我尝试使用python 3.6,但仍然无法正常工作

    Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "E:\Users\HCL\AppData\Local\Programs\Python\Python37\lib\site- 
 packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "E:\Users\HCL\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 347, in execute
    django.setup()
  File "E:\Users\HCL\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "E:\Users\HCL\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "E:\Users\HCL\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "E:\Users\HCL\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "E:\Users\HCL\Desktop\learning_log\learning_logs\models.py", line 10, in <module>
    class Entry(models.Model):
  File "E:\Users\HCL\Desktop\learning_log\learning_logs\models.py", line 11, in Entry
    topic =  models.ForeignKey(Topic)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

1 个答案:

答案 0 :(得分:3)

在模型Entry

中更改此行
topic =  models.ForeignKey(Topic)

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