Home > Blockchain >  Django import my app models AppRegistryNotReady
Django import my app models AppRegistryNotReady

Time:04-07

I will import my models in celery.py. But when I import and run the runserver command, I get the following error:

  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.

My import code line in celery.py: from app.models import model1, model2

CodePudding user response:

You can try adding this line to your settings.py file:

import django
django.setup()

See here for solution.

if it doesn't work, this might be what you need:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")

and these lines:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

If the first solution doesn't work here the alternative solution.

Finally, there is 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!!

CodePudding user response:

I solved. Crop all tasks in celery.py, pasted in app tasks.py :)

It also fixes itself when i delete celery related lines from init.py file

  • Related