Django 1.9 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet
Question: Trying to use django-shopify-sync in a Django 1.9 project. When loading the
config for the app it gives me the following error, likely because it's trying
to load some models in the config?

Tried moving the the two imports that eventually import models into the
ready() function below, but still getting the same error. Culpirt lines 2 and
3 in the following file <https://github.com/andresdouglas/django-shopify-
sync/blob/master/shopify_sync/apps.py>

The error is:

    
    
        $ python manage.py runserver
    Unhandled exception in thread started by <function wrapper at 0x10753e500>
    Traceback (most recent call last):
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
        six.reraise(*_exception)
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
        app_config = AppConfig.create(entry)
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/apps/config.py", line 116, in create
        mod = import_module(mod_path)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
      File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/apps.py", line 2, in <module>
        from shopify_sync.handlers import webhook_received_handler
      File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/handlers.py", line 3, in <module>
        from .models import (CustomCollection, Customer, Order, Product, Shop,
      File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/models/__init__.py", line 3, in <module>
        from .address import Address
      File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/models/address.py", line 6, in <module>
        from .base import ShopifyResourceModel
      File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/models/base.py", line 144, in <module>
        class ShopifyResourceModel(models.Model):
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
        app_config = apps.get_containing_app_config(module)
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
        self.check_apps_ready()
      File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
        raise AppRegistryNotReady("Apps aren't loaded yet.")
    django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    

**UPDATE** : If I move the following lines (model imports)
<https://github.com/andresdouglas/django-shopify-
sync/blob/master/shopify_sync/handlers.py#L3-L4> to inside `get_topic_models`
it seems to fix the error. But that's kind of dirty, can anyone come up with a
better solution?


Answer: ~~It looks like you may have an ordering issue. Make sure your application is
in the`INSTALLED_APPS` tuple after `django-shopify-sync`. You can find a few
more details in the [Application registry
documentation](https://docs.djangoproject.com/en/1.9/ref/applications/#application-
registry).~~

As unsatisfying as an inline import is, you may be stuck with it. I'd suggest
moving

    
    
    from shopify_sync.handlers import webhook_received_handler
    from shopify_webhook.signals import webhook_received
    

into the `ready` method in `apps.py`. This will delay the import until the
models are ready.

The change I tried is:

    
    
    diff --git a/shopify_sync/apps.py b/shopify_sync/apps.py
    index 663b43b..0bc1fcc 100644
    --- a/shopify_sync/apps.py
    +++ b/shopify_sync/apps.py
    @@ -1,7 +1,5 @@
     from django.apps import AppConfig
    -from shopify_sync.handlers import webhook_received_handler
    -from shopify_webhook.signals import webhook_received
    -
    +import importlib
    
     class ShopifySyncConfig(AppConfig):
         """
    @@ -16,5 +14,9 @@ class ShopifySyncConfig(AppConfig):
             The ready() method is called after Django setup.
             """
    
    +        signals_webhook_received = importlib.import_module('.signals', package='shopify_webhook')
    +        handlers_webhook_received_handler = importlib.import_module('.handlers', package='shopify_sync')
    +
             # Connect shopify_webhook's webhook_received signal to our synchronisation handler.
    -        webhook_received.connect(webhook_received_handler, dispatch_uid = 'shopify_sync_webhook_received_handler')
    +        signals_webhook_received.webhook_received.connect(handlers_webhook_received_handler.webhook_received_handler, dispatch_uid = 'shopify_sync_webhook_received_handler')
    + 
    

