Django issue : django.core.exceptions.AppRegistryNotReady (Apps aren't loaded yet)

Feature(s) impacted

I get this error django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet when trying to start a django server with forestadmin.

Observed behavior

When trying to start the django server either with uwgi.py or manage.py runserver I get the same error about forestadmin app initialization.

I read a similar issue here but the fix “move the init_forest call below the get_wsgi_application call” does not work for me.

Failure Logs

Failed log for python manage.py runserver :

Traceback (most recent call last):
  File "/app/manage.py", line 31, in <module>
    main()
  File "/app/manage.py", line 24, in main
    from django_forest import init_forest
  File "/usr/local/lib/python3.10/site-packages/django_forest/__init__.py", line 3, in <module>
    from django_forest.utils.schema import Schema
  File "/usr/local/lib/python3.10/site-packages/django_forest/utils/schema/__init__.py", line 30, in <module>
    class Schema:
  File "/usr/local/lib/python3.10/site-packages/django_forest/utils/schema/__init__.py", line 37, in Schema
    'database_type': connection.vendor,
  File "/usr/local/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/usr/local/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 208, in create_connection
    backend = load_backend(db["ENGINE"])
  File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 8, in <module>
    from .operations import PostGISOperations
  File "/usr/local/lib/python3.10/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 19, in <module>
    from .models import PostGISGeometryColumns, PostGISSpatialRefSys
  File "/usr/local/lib/python3.10/site-packages/django/contrib/gis/db/backends/postgis/models.py", line 8, in <module>
    class PostGISGeometryColumns(models.Model):
  File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Failed log for uwsgi :

Traceback (most recent call last):
  File "/home/bas/app/backend/./crawling/wsgi.py", line 15, in <module>
    application = get_wsgi_application()
  File "/home/bas/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/bas/venv/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/bas/venv/lib/python3.10/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/bas/venv/lib/python3.10/site-packages/django/apps/config.py", line 228, in create
    import_module(entry)
  File "/usr/x86_64-pc-linux-gnu/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/bas/venv/lib/python3.10/site-packages/django_forest/__init__.py", line 3, in <module>
    from django_forest.utils.schema import Schema
  File "/home/bas/venv/lib/python3.10/site-packages/django_forest/utils/schema/__init__.py", line 30, in <module>
    class Schema:
  File "/home/bas/venv/lib/python3.10/site-packages/django_forest/utils/schema/__init__.py", line 37, in Schema
    'database_type': connection.vendor,
  File "/home/bas/venv/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/home/bas/venv/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/home/bas/venv/lib/python3.10/site-packages/django/db/utils.py", line 208, in create_connection
    backend = load_backend(db["ENGINE"])
  File "/home/bas/venv/lib/python3.10/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "/usr/x86_64-pc-linux-gnu/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/bas/venv/lib/python3.10/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 8, in <module>
    from .operations import PostGISOperations
  File "/home/bas/venv/lib/python3.10/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 19, in <module>
    from .models import PostGISGeometryColumns, PostGISSpatialRefSys
  File "/home/bas/venv/lib/python3.10/site-packages/django/contrib/gis/db/backends/postgis/models.py", line 8, in <module>
    class PostGISGeometryColumns(models.Model):
  File "/home/bas/venv/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/bas/venv/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/bas/venv/lib/python3.10/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")

Context

I have a Postgis database 14.2, python 3.10, django 4.0.4 and django-forestadmin 1.3.2

settings.py

# Forest admin settings
FOREST_ENABLED = config("FOREST_ENABLED", default=False, cast=bool)
if FOREST_ENABLED:
    # Must be in first position
    INSTALLED_APPS.insert(0, "django_forest")
    FOREST = {
        "FOREST_URL": "https://api.forestadmin.com",
        "APPLICATION_URL": "<my site>",
        "FOREST_ENV_SECRET": config("FOREST_ENV_SECRET", default=""),
        "FOREST_AUTH_SECRET": config("FOREST_AUTH_SECRET", default=""),
    }
    APPEND_SLASH = False

wsgi.py

"""
WSGI config for crawling project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

FOREST_ENABLED = os.environ.get("FOREST_ENABLED", "False") == "True"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crawling.settings.prod")

application = get_wsgi_application()

# Initializing forest after wsgi application, and only if FOREST_ENABLED
if FOREST_ENABLED:
    from django_forest import init_forest

    init_forest()

urls.py

if settings.FOREST_ENABLED:
    urlpatterns += path("forest", include("django_forest.urls"))

Hi @Jim,

It’s not clear for me, I have to investigate your issue.
Are you sure that you don’t call init_forest in another file ?

Hello @valentinm ,

The only place with init_forest() is in wsgi.py. You can check in the traceback.
I can add more log/print in forestadmin if you want to check for other call ?

Hello @Jim

I don’t reproduce your issue for the moment.
I need more time to understand whats happened.