我试图从另一个python脚本访问我的Django(v1.10)应用程序数据库,但遇到了一些问题.
这是我的文件和文件夹 struct :
store
store
__init.py__
settings.py
urls.py
wsgi.py
store_app
__init.py__
admin.py
apps.py
models.py
...
db.sqlite3
manage.py
other_script.py
根据Django's documentations my other_script.py
看起来像这样:
import django
from django.conf import settings
settings.configure(DEBUG=True)
django.setup()
from store.store_app.models import MyModel
但它会生成一个运行时错误:
RunTimeError: Model class store.store_app.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
我应该注意到,我的INSTALLED_APPS
列表包含store_app
作为其最后一个元素.
如果我try 传递这样的配置:
import django
from django.conf import settings
from store.store_app.apps import StoreAppConfig
settings.configure(StoreAppConfig, DEBUG=True)
django.setup()
from store.store_app.models import MyModel
我得到了:
AttributeError: type object 'StoreAppConfig has no attribute 'LOGGING_CONFIG'.
如果我编辑settings.py
并添加LOGGING_CONFIG=None
,我会得到另一个关于缺少属性的错误,以此类推.
如有任何建议,将不胜感激.