我的项目在django 1.11
中运行良好这是我的manage.py文件
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myfoodsite.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
直到我将django升级到django 2.0.0
使用pip install python=2.0.0
,输出如下
(myvenv) shubhendu@shubhendu-HP-Pavilion-g6-Notebook-PC:/home/foodballbear$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/foodballbear/block/models.py", line 41, in <module>
class Post(models.Model):
File "/home/foodballbear/block/models.py", line 42, in Post
author = models.ForeignKey('auth.User')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
我现在在Django 1.11上开发得很好但是如何让它在Django 2.0上运行?
答案 0 :(得分:1)
由于Django 2.0中需要ForeignKey in Django 1.1个状态on_delete的文档
ForeignKey
一个 多对一的关系。需要两个位置参数:类 与模型相关的内容和on_delete选项。 (on_delete 实际上并不需要,但不提供它会给予弃用 警告。它将在Django 2.0中使用。)
此外,您的错误指向相同的
author = models.ForeignKey('auth.User')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
您应该在每个外键上设置on_delete属性,使其与Django 2.0兼容