1
我是django的新手。想要在我的项目的数据库中创建表格,所以我搜索了教程。 models.py:无法使用django修改数据库
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.
我已经将应用程序添加到INSTALLED_APPS,并且能够使用“manage.py shell”执行完全相同的命令,但不能与脚本一起执行。
我在这里错过了什么?
感谢
您将需要至少设置“DJANGO_SETTINGS”和“django.setup()”......你究竟在做什么......这通常不是你如何将任何东西插入到你的django db ...看到https://docs.djangoproject.com/en/1.9/howto/initial-data/,因为它似乎与我认为你正在尝试做的事情非常相关... –
如果你确实需要脚本针对Django(和ORM),您需要使用[shell管理命令](https://docs.djangoproject.com/es/1.9/ref/django-admin/#shell),将脚本作为输入到它。 [相关问题](http://stackoverflow.com/questions/16853649/executing-python-script-from-django-shell) – mragh
非常感谢你的答案!通过脚本作为参数对我来说工作得很好。 –