gpt4 book ai didi

python - "django.core.exceptions.AppRegistryNotReady: Apps aren' t尚未加载。”用于启动代码

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:43 24 4
gpt4 key购买 nike

因此,我尝试向 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 很长时间了(不确定我是否接触过任何旧版本) 。知道发生了什么吗?

项目名称/应用程序名称/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

项目名称/应用程序名称/__init__.py:

default_app_config = 'appName.apps.AppNameConfig'

项目名称/应用程序名称/models.py:

from django.db import models

class Country(models.Model):
#...

class Embassy(models.Model):
#...

最佳答案

正如 @Andrey Shipilov 所评论的,这是一个错误导入的情况,因为您无法在与 AppConfig 类相同的级别上导入。我的解决方案的修复是将导入移至 AppConfig.ready()

项目名称/应用程序名称/apps.py:

from django.apps import AppConfig


class FinderConfig(AppConfig):
import finder.osm as osm
name = 'finder'
init = True
def ready(self):
pass

关于python - "django.core.exceptions.AppRegistryNotReady: Apps aren' t尚未加载。”用于启动代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com