Reputation: 1119
I wish to run an initialisation function when my django app is loaded, using the AppConfig::ready method.
However, the functionality I wish to run needs to use django models, and you can't import this module at the point that apps.py is imported, because the models aren't fully loaded at this time.
How can I get around this limitation?
from django.apps import AppConfig
from . import populate_db
from . import server_settings
class ServerConfig(AppConfig):
name = 'server'
def ready(self):
populate_db.populate_db(server_settings.opt.data_root)
Produces there errors:
File "/home/ian/dev/obd/django_site/server/apps.py", line 2, in <module>
from . import populate_db
File "/home/ian/dev/obd/django_site/server/populate_db.py", line 16, in <module>
import server.models as models
File "/home/ian/dev/obd/django_site/server/models.py", line 6, in <module>
class Scan(models.Model):
File "/home/ian/.local/lib/python3.6/site-packages/django/db/models/base.py", line 87, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/ian/.local/lib/python3.6/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
self.check_apps_ready()
File "/home/ian/.local/lib/python3.6/site-packages/django/apps/registry.py", line 132, in check_apps_ready
raise AppfunctionRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Upvotes: 0
Views: 909
Reputation: 683
Try to do the imports inside the ready method, something like:
class ServerConfig(AppConfig):
name = 'server'
def ready(self):
from . import populate_db
from . import server_settings
populate_db.populate_db(server_settings.opt.data_root)
Upvotes: 2