Django 导入我的应用模型 AppRegistryNotReady
Django import my app models AppRegistryNotReady
我将在 celery.py 中导入我的模型。但是,当我导入 运行 运行 服务器命令时,出现以下错误:
File "/directory/manage.py", line 22, in <module>
main()
File "/directory/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 363, in execute
settings.INSTALLED_APPS
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
......
File "/directory/__init__.py", line 1, in <module>
from .celery import app as celery_app
File "/directory/celery.py", line 9, in <module>
from apps.models import Model1
File "/directory/apps/models.py", line 2, in <module>
from django.contrib.auth.models import User, AbstractUser
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/directory/venv/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
我在 celery.py 中的导入代码行:
from app.models import model1, model2
您可以尝试将此行添加到您的 settings.py 文件中:
import django
django.setup()
如果它不起作用,这可能是您需要的:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
和这些行:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
如果第一个解决方案在这里不起作用 alternative solution.
最后还有such a solution:
import django
# some variable declarations
world_mapping = {
'osm_id': 'osm_id',
}
if __name__ == '__main__':
django.setup()
# import AFTER setup
from app.models import WorldBorder
# from now I can access WorldBorder!!
我通过将所有任务从 celery.py
复制到应用程序 tasks.py
来解决它。
当我从 __init__.py
文件中删除与 celery 相关的行时,它也会自行修复。
我将在 celery.py 中导入我的模型。但是,当我导入 运行 运行 服务器命令时,出现以下错误:
File "/directory/manage.py", line 22, in <module>
main()
File "/directory/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 363, in execute
settings.INSTALLED_APPS
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
......
File "/directory/__init__.py", line 1, in <module>
from .celery import app as celery_app
File "/directory/celery.py", line 9, in <module>
from apps.models import Model1
File "/directory/apps/models.py", line 2, in <module>
from django.contrib.auth.models import User, AbstractUser
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/directory/venv/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
我在 celery.py 中的导入代码行:
from app.models import model1, model2
您可以尝试将此行添加到您的 settings.py 文件中:
import django
django.setup()
如果它不起作用,这可能是您需要的:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
和这些行:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
如果第一个解决方案在这里不起作用 alternative solution.
最后还有such a solution:
import django
# some variable declarations
world_mapping = {
'osm_id': 'osm_id',
}
if __name__ == '__main__':
django.setup()
# import AFTER setup
from app.models import WorldBorder
# from now I can access WorldBorder!!
我通过将所有任务从 celery.py
复制到应用程序 tasks.py
来解决它。
当我从 __init__.py
文件中删除与 celery 相关的行时,它也会自行修复。