Django 1.9 django.core.exceptions.AppRegistryNotReady:尚未加载应用程序

时间:2016-05-11 00:28:25

标签: python django shopify django-1.9

尝试在Django 1.9项目中使用django-shopify-sync。加载应用程序的配置时,它会给我以下错误,可能是因为它试图在配置中加载某些模型?

尝试将最终导入模型的两个导入移动到下面的ready()函数中,但仍然得到相同的错误。 Culpirt第2行和第3行位于以下文件https://github.com/andresdouglas/django-shopify-sync/blob/master/shopify_sync/apps.py

错误是:

    $ 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.

更新:如果我将以下行(模型导入)https://github.com/andresdouglas/django-shopify-sync/blob/master/shopify_sync/handlers.py#L3-L4移到get_topic_models内,则似乎可以修复错误。但那种肮脏的,有人能提出更好的解决方案吗?

2 个答案:

答案 0 :(得分:0)

看起来您可能有订购问题。确保您的应用程序位于INSTALLED_APPS之后的django-shopify-sync元组中。您可以在Application registry documentation

中找到更多详细信息

如同内联导入一样令人不满意,您可能会被它困住。我建议搬家

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

进入ready中的apps.py方法。这将延迟导入,直到模型准备就绪。

我尝试的改变是:

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')
+ 

答案 1 :(得分:0)

如果移动以下行(模型导入)https://github.com/andresdouglas/django-shopify-sync/blob/master/shopify_sync/handlers.py#L3-L4

from .models import (CustomCollection, Customer, Order, Product, Shop,
                 SmartCollection)

get_topic_models内,似乎可以修复错误。但那种肮脏的,有人能提出更好的解决方案吗?