无法使用django修改DataBase

时间:2016-02-05 00:45:31

标签: 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()

将数据写入db:

的脚本
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.

我已经将应用程序添加到INSTALLED_APPS中,我可以使用&#34; manage.py shell&#34;执行完全相同的命令,但不能使用脚本。

我在这里缺少什么?

由于

1 个答案:

答案 0 :(得分:1)

如果您确实需要针对Django(和ORM)编写脚本,那么您将要使用shell admin command,将脚本作为输入传递给它。 Related question