zoukankan      html  css  js  c++  java
  • django 中出现TypeError: __init__() missing 1 required positional argument: 'on_delete'

    错误:

    C:UsersAdministratorDesktopcode_releaseappsmodels.py changed, reloading.
    Watching for file changes with StatReloader
    Exception in thread django-main-thread:
    Traceback (most recent call last):
      File "D:软件Python 3.6.4lib	hreading.py", line 916, in _bootstrap_inner
        self.run()
      File "D:软件Python 3.6.4lib	hreading.py", line 864, in run
        self._target(*self._args, **self._kwargs)
      File "D:软件Python 3.6.4libsite-packagesdjangoutilsautoreload.py", line 53, in wrapper
        fn(*args, **kwargs)
      File "D:软件Python 3.6.4libsite-packagesdjangocoremanagementcommands
    unserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "D:软件Python 3.6.4libsite-packagesdjangoutilsautoreload.py", line 76, in raise_last_exception
        raise _exception[1]
      File "D:软件Python 3.6.4libsite-packagesdjangocoremanagement\__init__.py", line 357, in execute
        autoreload.check_errors(django.setup)()
      File "D:软件Python 3.6.4libsite-packagesdjangoutilsautoreload.py", line 53, in wrapper
        fn(*args, **kwargs)
      File "D:软件Python 3.6.4libsite-packagesdjango\__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "D:软件Python 3.6.4libsite-packagesdjangoapps
    egistry.py", line 114, in populate
        app_config.import_models()
      File "D:软件Python 3.6.4libsite-packagesdjangoappsconfig.py", line 211, in import_models
        self.models_module = import_module(models_module_name)
      File "D:软件Python 3.6.4libimportlib\__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 "C:UsersAdministratorDesktopcode_releaseappsmodels.py", line 36, in <module>
        class DeployTask(models.Model):
      File "C:UsersAdministratorDesktopcode_releaseappsmodels.py", line 39, in DeployTask
        project = models.ForeignKey(verbose_name='项目环境', to='Project')
    TypeError: __init__() missing 1 required positional argument: 'on_delete'

    解决:

    在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
    TypeError: __init__() missing 1 required positional argument: 'on_delete'

       project = models.ForeignKey(verbose_name='项目环境', to='Project', on_delete=models.CASCADE)
  • 相关阅读:
    iOS-AVPlayer
    简单认识一下什么是闭包
    你珍惜一个人,一定是感动过
    用户关闭浏览器页器,弹出一个提示
    Html5 小球键盘移动
    HTML坦克大战学习01
    關於 WebClient wc = new WebClient() 下載第三方數據不能進安安信任異常
    封装自己的yQuery
    js彈出層或者js彈出引用url Frame 層
    动态创建表单模拟提交
  • 原文地址:https://www.cnblogs.com/HByang/p/12751327.html
Copyright © 2011-2022 走看看