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

时间:2016-02-28 21:19:19

标签: python django django-models django-signals

我遇到了常见的“模型尚未加载”错误,该错误源于将单独的信号文件导入我的应用models.py。我找到了2个关于如何创建AppConfig并在ready()函数中导入信号的简单指南,但我无法使其正常工作。我使用的是最常见的答案here以及this博文。

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

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

这是缩写,但我已经尝试在apps.py__init.py__下放置funproj所需的app和相关代码,但这两种方法都不起作用。这就是我对每一个人的看法:

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,我认为它应该是,或者他们是否将app用作项目的同义词。

我认为这不会很难解决,我只是无法弄清楚我做错了什么。

编辑:这是使用Django 1.8的Python 3.4。下面的回溯,当我只有一个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',
)

2 个答案:

答案 0 :(得分:5)

您似乎同时导入了/*Set Slim up*/ $container = new \Slim\Container();//Ver si le afecta o no los paréntesis () $container['settings'] = function($c){ return $c['config']->get('settings'); }; $container['config'] = function($c){ //c= current state of our container return new \Noodlehaus\Config('../config/app.php');//We could pass more parameters ... as an array ['param1','param2','etc'] }; $container['view'] = function($c){//c= current state of our container $view = new Slim\Views\Twig('../resources/views');//Ubicación de las vistas //Ability to generate URLs to routes from within our views $view->addExtension(new \Slim\Views\TwigExtension( $c['router'], $c['config']->get('url') )); return $view; }; //base de datos: $container['db'] = function($c){ //return new PDO('mysql:host=127.0.0.1;dbname=destructy','root','root'); return new PDO( 'mysql:host='.$c['config']->get('db.mysql.host').';dbname='.$c['config']->get('db.mysql.dbname'), $c['config']->get('db.mysql.username'), $c['config']->get('db.mysql.password')); }; $app = new Slim\App($container); require_once 'routes.php'; $app->get('/',function($request,$response,$args){ echo 'Destructy - Home.'; echo '<p style="color: darkblue">Welcome to the admin area</p>'; echo '<p>Testing access to config parameters ...</p>'; echo $this->config->get('db.mysql.host').'<br>'; echo $this->config->get('url'); /*Testing out DB connection*/ var_dump($this->db); $this->view->render($response,'home.twig'); }); $app->run(); 信号。加载apps.py后会导入models.py,然后尝试引用您的models.py模型,导致“模型尚未加载”错误。

signals移除Game。您可以从from app import signals导入models.py,但不能从另一端导入。

答案 1 :(得分:0)

试试这个:

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