将数据库同步到 python django
Posted
技术标签:
【中文标题】将数据库同步到 python django【英文标题】:sync database to python django 【发布时间】:2019-03-11 17:31:17 【问题描述】:我正在尝试将 SQLite3 数据库连接到 Django。当我尝试使用命令同步数据库时
$ python manage.py syncdb
它显示了很长的痕迹。我不知道如何解决它。谁能帮我解决这个问题
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 985, in _gcd_import
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 697, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "F:\django\DatabaseConnectTest\polls\models.py", line 9, in <module>
class Choice(models.Model):
File "F:\django\DatabaseConnectTest\polls\models.py", line 10, in Choice
quesion = models.ForeignKey(Question)
TypeError: __init__() missing 1 required positional argument: 'on_delete'
【问题讨论】:
【参考方案1】:自 Django 2.0 起,所有 ForeignKey 字段都需要 on_delete
参数。不再有默认值。您必须遍历所有模型并将此参数添加到关系中。
回溯会告诉你在哪里可以找到应该修复的代码。
File "F:\django\DatabaseConnectTest\polls\models.py", line 10, in Choice
quesion = models.ForeignKey(Question)
在大多数情况下,on_delete=models.CASCADE
是您想要的。
question = models.ForeignKey(Question, on_delete=models.CASCADE)
https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey.on_delete
【讨论】:
以上是关于将数据库同步到 python django的主要内容,如果未能解决你的问题,请参考以下文章
python测试开发django-72.删除表后如何重新生成表