[英]Django: having trouble setting up app ready() to import signals
I'm hitting the common "Models aren't loaded yet" error that arises from having a separate signals file that is imported into my apps models.py
. 我遇到了常见的“尚未加载模型”错误,该错误是由于将单独的信号文件导入到我的应用程序
models.py
。 I found 2 easy guides on how to create an AppConfig and import signals in the ready() function, but I can't get it working. 我找到了2条有关如何创建AppConfig和如何在ready()函数中导入信号的简单指南,但是我无法使其正常运行。 I'm using the top answer here as well as this blog post.
我在这里以及这篇博客文章中都使用了最佳答案。
Part of this is likely due to the fact that my entire project has a name, funproj
, and the main app where the bulk of the code is located is helpfully called app
- I'm using Visual Studio with Python Tools for VS and it generated this structure when I created the project. 这部分可能是由于我的整个项目有一个名字,其实
funproj
,主要应用在代码所在的大部分是有益称为app
-我使用Visual Studio中,它产生Python的工具VS创建项目时使用的结构。 Here's the hierarchy: 这是层次结构:
funproj
|--- __init__.py
|--- setttings.py
|--- urls.py
|
|--app
| |--- __init__.py
| |--- models.py
| |--- signals.py
|
|--otherapp
| |--- __init__.py
| |--- other stuff
This is abbreviated, but I've tried putting the required apps.py
and relevant code for __init.py__
under both funproj
and app
but neither method works. 这是缩写,但我尝试将所需的
apps.py
和apps.py
相关代码__init.py__
funproj
和app
但两种方法均funproj
。 This is what I have for each of those: 这是我为每个人所拥有的:
apps.py apps.py
from django.apps import AppConfig
class FunProjAppConfig(AppConfig):
name = 'app'
def ready(self):
from app import signals
__init.py__ addition __init.py__添加
#import signals in the app config
default_app_config = 'app.apps.FunProjAppConfig'
It makes more sense to me to have this code in app
since it contains the signals that need to be loaded, however the examples I've linked aren't clear to me. 它更有意义,我有这个代码
app
,因为它包含了需要加载的信号,然而我已经链接的例子是,我不清楚。 One mentions the 'app directory' but I'm not sure if that literally means the app
, as I think it should, or if they're using app as a synonym for project. 有人提到“应用程序目录”,但我不确定这是否真的意味着
app
,正如我认为的那样,还是他们将app用作项目的同义词。
I don't think this is going to be hard to fix, I just can't figure out what I'm doing wrong. 我认为这很难解决,只是无法弄清楚我在做什么错。
EDIT: This is Python 3.4 with Django 1.8. 编辑:这是Python 3.4和Django 1.8。 Traceback below, it's the same error I get when I simply have a
signals.py
file and an import in `models.py' of those signals. 在下面的Traceback中,这是我仅拥有一个
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: I added a print
to the ready()
function but saw no output. EDIT2:我将
print
添加到ready()
函数中,但未看到输出。 Here are my installed apps: 这是我安装的应用程序:
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',
)
It looks like you have both apps.py
and models.py
importing your signals. 看来您同时导入了
apps.py
和models.py
信号。 When models.py
is loaded it imports signals
which then tries to reference your Game
model, causing the "Models aren't yet loaded" error. 加载
models.py
,它将导入signals
,然后尝试引用您的Game
模型,从而导致“尚未加载模型”错误。
Remove the from app import signals
from models.py
. 从
models.py
删除from app import signals
。 You can import your models
from signals
but not the other way around. 您可以从
signals
导入models
,但不能反过来。
尝试这个:
@receiver(post_save, sender='app.Game')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.