Description
When importing a notification class like EmailNotification from herald.base during application startup, Django raises an AppRegistryNotReady exception. This occurs because herald.base attempts to load the SentNotification model at the module level, which happens before the Django app registry is fully initialized.
This can be triggered by importing a notifications.py file early in the startup process.
Traceback
web-1 | File "/code/shared/herald.py", line 5, in <module>
web-1 | from herald.base import EmailNotification
web-1 | File "/code/.venv/lib/python3.10/site-packages/herald/base.py", line 21, in <module>
web-1 | SentNotification = get_sent_notification_model()
web-1 | File "/code/.venv/lib/python3.10/site-packages/herald/utils.py", line 11, in get_sent_notification_model
web-1 | return django_apps.get_model(sent_notification)
web-1 | File "/code/.venv/lib/python3.10/site-packages/django/apps/registry.py", line 199, in get_model
web-1 | self.check_models_ready()
web-1 | File "/code/.venv/lib/python3.10/site-packages/django/apps/registry.py", line 141, in check_models_ready
web-1 | raise AppRegistryNotReady("Models aren't loaded yet.")
web-1 | django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Cause
The file /code/shared/herald.py calls get_sent_notification_model() at the module level:
# herald/base.py
from herald.utils import get_sent_notification_model
SentNotification = get_sent_notification_model() # <--- This line executes on import
This is problematic because any module that imports from herald.base will trigger this model lookup immediately. If the import happens before Django has loaded all applications, it will fail.
This can affect the admin.py and the delnotifs management command as well due to their useage of get_sent_notification_model().
Description
When importing a notification class like EmailNotification from herald.base during application startup, Django raises an AppRegistryNotReady exception. This occurs because herald.base attempts to load the SentNotification model at the module level, which happens before the Django app registry is fully initialized.
This can be triggered by importing a notifications.py file early in the startup process.
Traceback
Cause
The file
/code/shared/herald.pycallsget_sent_notification_model()at the module level:This is problematic because any module that imports from herald.base will trigger this model lookup immediately. If the import happens before Django has loaded all applications, it will fail.
This can affect the
admin.pyand thedelnotifsmanagement command as well due to their useage ofget_sent_notification_model().