Django(& Pinax):追溯“AppRegistryNotReady:尚未加载应用程序。”例外

时间:2015-03-02 22:25:03

标签: python django pinax

我正在使用Django 1.7.5,并尝试构建pinax-project-teams启动器。运行python manage.py check时,我会遇到`django.core.exceptions.AppRegistryNotReady:尚未加载应用。'。

我已经在StackOverflow和其他地方阅读了类似的错误,似乎这个错误来自各种原因,并且往往涉及特定环境的修复。所以我可以提前删除几个解决方案:1)这是从以前的Django版本升级; 2)因此,wsgi.py正确使用较新的from django.core.wsgi import get_wsgi_application

完整的堆栈跟踪:

(env)trevor@nikola:webapp.git$ python manage.py check
Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    startup.run()
  File "/Users/trevor/zenith/webapp.git/djangoapp/startup.py", line 22, in run
    admin.autodiscover()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/usr/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 67, in autodiscover_modules
for app_config in apps.get_app_configs():
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/usr/local/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.

错误源于pinax的startup.run(),其中包含两个调用:autoload(["receivers"]),用于加载settings.INSTALLED_APPS中的模块和admin.autodiscover()。这是自动加载:

def autoload(submodules):
    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # print('Module: \t\n%s' % str(mod))
        for submodule in submodules:
            # print('Submodule: \t\n%s\n' % str(submodule))
            try:
                import_module("{0}.{1}".format(app, submodule))
            except:
                if module_has_submodule(mod, submodule):
                    raise

通过INSTALLED_APPS并成功导入列出的所有应用,但随后admin.autodiscover()抛出Apps aren't loaded yet.消息。我不确定我错过了什么......很明显,很可能!

哦,最后:我已经阅读了import django; django.setup()修复程序,但我认为这是针对脚本和互动,而不是django项目?因为我尝试运行交互式,导入我的项目设置,运行configure(),运行django.setup(),然后退出,但是当我尝试执行runserver时,这对上述问题没有影响......: - /想法?

2 个答案:

答案 0 :(得分:3)

编辑 - 更新&amp;总结 pinax-project-teams代码与Django 1.7不完全兼容,至少不是在新的Django项目的初始“原始”安装。解决方法包括几个步骤:

  1. startup.pyline 21, in run())移除明确/冗余应用加载:注释或删除admin.autodiscover()
  2. 明确安装Django 1.6.5 pip install Django==1.6.5)。
  3. 通过django 1.6版本(python manage.py syncdb)同步数据库。
  4. 明确安装Django 1.7.5 pip install Django==1.7.5)。
  5. 原帖答案: 我想我已经缩小了一点,如果不是在范围内,至少在根本原因。虽然我的项目不是从Django&lt; = 1.6升级到1.7,但这仍然是一个问题。 pinax-project-team中的某些内容使其默认与Django 1.7 不兼容。在 1.6.5 django安装上,相同的安装将允许syncdb然后runserver,但django-1.7.5会抛出Apps aren't loaded yet

    我找到了一个很好的长Django 1.7 release notes列表,我正在开始查看是否可以确定不兼容的原因。

    至少有一个问题是Pinax在startup.py中的“额外”自动发现,as of 1.7 release is automatically executed在Django自己的启动过程中。这解决了应用加载问题,看起来......但django.contrib.sites还有另一个问题。在1.7中运行migrate时,由于数据库中缺少表,其中一个迁移失败,特别是django_site(下面的完整跟踪)。此表 存在于1.6 django安装的db中。修改1.7的设置以指向1.6项目中的sqlite文件解决了这个问题,并且瞧,它的工作原理。所以这是我在pinax-project-teams和Django 1.7之间发现的唯一两个冲突。

    (env)trevor@nikola:mysite7$ python manage.py migrate
    Operations to perform:
      Synchronize unmigrated apps: wiki, account, eventlog, kaleo, profiles, easy_thumbnails, pinax_theme_bootstrap, teams, bootstrapform
      Apply all migrations: admin, contenttypes, sites, auth, sessions
    Synchronizing apps without migrations:
      Creating tables...
      Installing custom SQL...
      Installing indexes...
    Traceback (most recent call last):
      File "manage.py", line 11, in <module>
        execute_from_command_line(sys.argv)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
        self.execute(*args, **options.__dict__)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
        output = self.handle(*args, **options)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
        created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 298, in sync_apps
        call_command('loaddata', 'initial_data', verbosity=self.verbosity, database=connection.alias, skip_validation=True, app_label=app_label, hide_empty=True)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
        return klass.execute(*args, **defaults)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
        output = self.handle(*args, **options)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 61, in handle
        self.loaddata(fixture_labels)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 91, in loaddata
        self.load_label(fixture_label)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 148, in load_label
        obj.save(using=self.using)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save
        models.Model.save_base(self.object, using=using, raw=True)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base
        updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/base.py", line 679, in _save_table
        forced_update)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/base.py", line 723, in _do_update
        return filtered._update(values) > 0
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/query.py", line 600, in _update
        return query.get_compiler(self.db).execute_sql(CURSOR)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
        cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
        cursor.execute(sql, params)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
        return self.cursor.execute(sql, params)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
        return self.cursor.execute(sql, params)
      File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
        return Database.Cursor.execute(self, query, params)
    django.db.utils.OperationalError: Problem installing fixture '/Users/trevor/code/pinax/7-pinaxtest/mysite7/fixtures/initial_data.json': Could not load sites.Site(pk=1): no such table: django_site
    

答案 1 :(得分:3)

postgresql和其他数据库之间的区别很重要。如果您正在使用SQLite后端,那么您只需复制数据库文件本身即可在多个位置重复使用。然后你可以有一个1.6项目和一个1.7项目,报告到同一个文件,而不需要一直升级和/或降级。