我見過很多關於如何在項目外使用Django模型的答案。但我沒有任何成功。我試圖執行this的答案,但我得到一個錯誤。我在我的應用程序中創建了一個新的「script.py」文件。從一個新的腳本訪問Django模型
script.py
from django.conf import settings
settings.configure(
DATABASE_ENGINE = 'sqlite3',
DATABASE_NAME = '/home/shivam/study/Python/automation/project/db.sqlite3',
DATABASE_USER = '',
DATABASE_PASSWORD = '',
DATABASE_HOST = '',
DATABASE_PORT = '',
TIME_ZONE = 'America/New_York',
)
from models import *
當我運行該腳本,我得到一個錯誤。
Traceback (most recent call last):
File "script.py", line 11, in <module>
from models import *
File "/home/shivam/study/Python/automation/project/videos/models.py", line 11, in <module>
class video(models.Model):
File "/home/shivam/study/Python/automation/env/local/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/shivam/study/Python/automation/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "/home/shivam/study/Python/automation/env/local/lib/python2.7/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.
有人可以幫助我嗎?
爲什麼不做管理命令? – RemcoGerlich