【问题标题】:Django 1.9 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yetDjango 1.9 django.core.exceptions.AppRegistryNotReady:应用程序尚未加载
【发布时间】:2016-05-11 00:28:25
【问题描述】:

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

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

错误是:

    $ 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 内部,它似乎可以修复错误。但这有点脏,谁能想出更好的解决方案?

【问题讨论】:

  • 这看起来确实是罪魁祸首,但在得到证实之前是无罪的。当您执行 ./manage.py runserver 或其他操作时,您是否看到此错误
  • @e4c5 运行服务器时,是的
  • 您已将 shopify-sync 注册为 INSTALLED_APPS 中的应用程序?
  • 正确。当我删除它时,它不会发生
  • 你能发布完整的回溯吗?

标签: python django shopify django-1.9


【解决方案1】:

如果您移动以下行(模型导入)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 内,它似乎修复了错误。但这有点脏,谁能想出更好的解决方案?

【讨论】:

    【解决方案2】:

    看来您可能有订购问题。确保您的应用程序位于django-shopify-sync 之后的INSTALLED_APPS 元组中。您可以在Application registry documentation 中找到更多详细信息。

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

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

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

    我尝试的改变是:

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

    【讨论】:

    • django-shopify-sync 是我要添加的应用程序。您指的是哪个其他应用程序?
    • 我的错,我误解了你的问题。让我看近一点。
    • @Andres :答案现在应该是相关的。
    • 感谢您的建议。我相信这实际上不起作用,因为在应用程序准备好之前会调用它。您必须将我在更新中提到的两行作为方法内的内联导入移动。你能想出一个更干净的方法吗?
    • 使用 Python 2.7 和 Django 1.9.6 似乎无需更改处理程序即可工作。
    猜你喜欢
    • 2016-08-04
    • 2016-03-06
    • 2016-01-08
    • 2017-09-16
    • 1970-01-01
    • 2018-11-29
    • 2018-06-18
    • 2016-09-26
    相关资源
    最近更新 更多