[英]Django: having trouble setting up app ready() to import signals
我遇到了常見的“尚未加載模型”錯誤,該錯誤是由於將單獨的信號文件導入到我的應用程序models.py
。 我找到了2條有關如何創建AppConfig和如何在ready()函數中導入信號的簡單指南,但是我無法使其正常運行。 我在這里以及這篇博客文章中都使用了最佳答案。
這部分可能是由於我的整個項目有一個名字,其實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
和apps.py
相關代碼__init.py__
funproj
和app
但兩種方法均funproj
。 這是我為每個人所擁有的:
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
,正如我認為的那樣,還是他們將app用作項目的同義詞。
我認為這很難解決,只是無法弄清楚我在做什么錯。
編輯:這是Python 3.4和Django 1.8。 在下面的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:我將print
添加到ready()
函數中,但未看到輸出。 這是我安裝的應用程序:
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.py
和models.py
信號。 加載models.py
,它將導入signals
,然后嘗試引用您的Game
模型,從而導致“尚未加載模型”錯誤。
從models.py
刪除from app import signals
。 您可以從signals
導入models
,但不能反過來。
嘗試這個:
@receiver(post_save, sender='app.Game')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.