Python 无法使用django修改数据库
我是django的新手。想要在我的项目的数据库中创建表,所以我搜索了教程。 models.py:Python 无法使用django修改数据库,python,django,python-2.7,django-models,Python,Django,Python 2.7,Django Models,我是django的新手。想要在我的项目的数据库中创建表,所以我搜索了教程。 models.py: from django.db import models class Blah(models.Model): first = models.IntegerField() second = models.IntegerField() 将数据写入数据库的脚本: from django.conf import settings settings.configure() from co
from django.db import models
class Blah(models.Model):
first = models.IntegerField()
second = models.IntegerField()
将数据写入数据库的脚本:
from django.conf import settings
settings.configure()
from core.models import Blah
b = Blah(first = 1, second = 2)
b.save()
当我尝试使用django 1.9启动脚本时,它会显示错误:
C:\Python27\Scripts\reports>to_db.py
Traceback (most recent call last):
File "C:\Python27\Scripts\reports\to_db.py", line 4, in <module>
from core.models import Blah
File "C:\Python27\Scripts\reports\core\models.py", line 5, in <module>
class Blah(models.Model):
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Python27\lib\site-packages\django\apps\registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "C:\Python27\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
C:\Python27\Scripts\reports>to\u db.py
回溯(最近一次呼叫最后一次):
文件“C:\Python27\Scripts\reports\to_db.py”,第4行,在
从core.models导入废话
文件“C:\Python27\Scripts\reports\core\models.py”,第5行,在
类Blah(models.Model):
文件“C:\Python27\lib\site packages\django\db\models\base.py”,第94行,在新的__
app\u config=apps.get\u包含app\u config(模块)
文件“C:\Python27\lib\site packages\django\apps\registry.py”,第239行,在包含应用程序配置的get_中
self.check_apps_ready()
文件“C:\Python27\lib\site packages\django\apps\registry.py”,第124行,在check\u apps\u ready中
raise AppRegistryNotReady(“应用程序尚未加载。”)
django.core.exceptions.AppRegistryNotReady:尚未加载应用程序。
我已经将该应用程序添加到已安装的应用程序中,我可以使用“manage.py shell”执行完全相同的命令,但不能使用脚本
我错过了什么
谢谢如果您确实需要针对Django(和ORM)编写脚本,您将需要使用,将脚本作为输入传递给它 您需要至少设置
DJANGO\u设置
和DJANGO.setup()
。。。你到底想做什么。。。这通常不是您将任何内容插入django数据库的方式。。。请看,因为它似乎与我假设您正在尝试的操作非常相关……如果您确实需要针对Django(和ORM)编写脚本,您将需要使用,将脚本作为输入传递给它。非常感谢您的回答!将脚本作为参数传递对我来说很好。