所以我试图在我的Django 1.10应用程序中添加一些代码,这些代码只会在服务器启动时运行。我从here和here接受了有关如何执行此操作的说明。每次我尝试运行python控制台时都会得到这个:
C:\Users\username\Envs\projectName [master ≡ +0 ~2 -0 !]> python manage.py shell
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 341, in execute
django.setup()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\username\AppData\Local\Programs\Python\Python3\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in_call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in_call_with_frames_removed
File "C:\Users\username\Envs\projectName\appName\apps.py", line 2, in <module>
import appName.osm as osm
File "C:\Users\username\Envs\projectName\appName\osm.py", line 4, in <module>
from .models import Country, Embassy
File "C:\Users\username\Envs\projectName\appName\models.py", line 38, in <module>
class Country(models.Model):
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\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.
我试过看了这个,但似乎所有得到这个例外的人都是因为它与Django的更新问题有关,但是我现在已经使用了1.10很长时间了(不确定我是否曾经碰过任何东西)以上)。知道发生了什么吗?
PROJECTNAME / APPNAME / apps.py:
from django.apps import AppConfig
import finder.osm as osm # this seems to cause the issue
class FinderConfig(AppConfig):
name = 'finder'
init = True
def ready(self):
pass
PROJECTNAME / APPNAME / __ INIT __ PY:
default_app_config = 'appName.apps.AppNameConfig'
PROJECTNAME / APPNAME / models.py:
from django.db import models
class Country(models.Model):
#...
class Embassy(models.Model):
#...
答案 0 :(得分:1)
正如@Andrey Shipilov评论的那样,这是一个糟糕的导入案例,因为你无法导入与AppConfig类相同的级别。我的解决方案的一个修复是将导入移动到AppConfig.ready()
PROJECTNAME / APPNAME / apps.py:
from django.apps import AppConfig
class FinderConfig(AppConfig):
import finder.osm as osm
name = 'finder'
init = True
def ready(self):
pass