Django:设置 app ready() 以导入信号时遇到问题

Django: having trouble setting up app ready() to import signals

我遇到了常见的 "Models aren't loaded yet" 错误,该错误是由于将单独的信号文件导入到我的应用程序 models.py 而引起的。我找到了 2 个关于如何在 ready() 函数中创建 AppConfig 和导入信号的简单指南,但我无法让它工作。我正在使用最佳答案 here as well as this 博客 post。

部分原因可能是我的整个项目都有一个名称 funproj,而大部分代码所在的主应用程序名为 app - 我我正在使用 Visual Studio 和 Python VS 工具,它在我创建项目时生成了这个结构。这是层次结构:

funproj
|--- __init__.py
|--- setttings.py
|--- urls.py
|
|--app
|   |--- __init__.py
|   |--- models.py
|   |--- signals.py
|
|--otherapp
|   |--- __init__.py
|   |--- other stuff

这是缩写,但我尝试将 apps.py__init.py__ 的相关代码放在 funprojapp 下,但两种方法均无效。这就是我对每个人的看法:

apps.py

from django.apps import AppConfig

class FunProjAppConfig(AppConfig):
  name = 'app'

  def ready(self):
    from app import signals

__init.py__加法

#import signals in the app config
default_app_config = 'app.apps.FunProjAppConfig'

对我来说,将这段代码放在 app 中更有意义,因为它包含需要加载的信号,但是我并不清楚我所链接的示例。有人提到 'app directory',但我不确定这是否如我认为的那样字面意思是 app,或者他们是否将应用程序用作项目的同义词。

我不认为这很难解决,我只是不知道我做错了什么。

编辑:这是 Python 3.4 和 Django 1.8。下面的回溯,当我只有一个 signals.py 文件并在这些信号的 `models.py' 中导入时,我得到了同样的错误。

C:\Python34\lib\importlib\_bootstrap.py:321: RemovedInDjango19Warning: The utili
ties in django.db.models.loading are deprecated in favor of the new application
loading system.
  return f(*args, **kwds)

Traceback (most recent call last):
  File "C:\Users\David\Source\Repos\path\to\projfiles\manage.py", line
17, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
312, in execute
    django.setup()
  File "C:\Python34\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python34\lib\site-packages\django\apps\registry.py", line 108, in pop
ulate
    app_config.import_models(all_models)
  File "C:\Python34\lib\site-packages\django\apps\config.py", line 198, in impor
t_models
    self.models_module = import_module(models_module_name)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\David\Source\Repos\path\to\projfiles\app\models.py", l
ine 11, in <module>
    from app import signals
  File "C:\Users\David\Source\Repos\path\to\projfiles\app\signals.py",
line 7, in <module>
    @receiver(post_save, sender=get_model('app', 'Game'))
  File "C:\Python34\lib\site-packages\django\apps\registry.py", line 199, in get
_model
    self.check_models_ready()
  File "C:\Python34\lib\site-packages\django\apps\registry.py", line 131, in che
ck_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

EDIT2:我在 ready() 函数中添加了一个 print 但没有看到任何输出。这是我安装的应用程序:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'kombu.transport.django',
    'accounts',
    'app',
    'scores',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    # Uncomment the next line to enable the admin:
     'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

您似乎同时 apps.pymodels.py 导入了您的信号。当加载 models.py 时,它会导入 signals 然后尝试引用您的 Game 模型,导致 "Models aren't yet loaded" 错误。

models.py 中删除 from app import signals。您可以从 signals 导入 models,但反过来不行。

试试这个:

@receiver(post_save, sender='app.Game')