AppRegistryNotReady: lazy format_html()?

5
为什么会出现这个异常?
Traceback (most recent call last):
  File "/path1/myapp-isu/myapp_isu/tests/unit/views/test_view_isu.py", line 8, in <module>
    from myapp_isu.search_form import ISUSearchForm
  File "/path1/myapp-isu/myapp_isu/search_form.py", line 87, in <module>
    class ISUSearchForm(forms.Form):
  File "/path1/myapp-isu/myapp_isu/search_form.py", line 108, in ISUSearchForm
    foo_filter=forms.ModelChoiceField(FooFilter.objects.all(), label=format_html('<a href="%s">%s</a>', reverse_lazy('foo-filter'), FooFilter._meta.verbose_name))
  File "/path1/dt/dt/utils/templateutils.py", line 127, in reverse
    return urlresolvers.reverse(*args, **kwargs)
  File "/path1/dt/dt/utils/urlresolverutils.py", line 49, in patched_reverse
    base_url = orig_reverse(viewname, urlconf=urlconf, args=args, kwargs=kwargs, prefix=prefix, current_app=current_app)
  File "/path2/django/core/urlresolvers.py", line 578, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/path2/django/core/urlresolvers.py", line 432, in _reverse_with_prefix
    self._populate()
  File "/path2/django/core/urlresolvers.py", line 284, in _populate
    for pattern in reversed(self.url_patterns):
  File "/path2/django/core/urlresolvers.py", line 401, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/path2/django/core/urlresolvers.py", line 395, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/path1/myapp-eins/myapp_eins/etc/rooturls.py", line 13, in <module>
    admin.autodiscover()
  File "/path2/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/path2/django/utils/module_loading.py", line 67, in autodiscover_modules
    for app_config in apps.get_app_configs():
  File "/path2/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/path2/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.

只有当我通过PyCharm调用unittest时才会发生,而在shell上使用py.test则不会。

我猜reverse_lazy()在这里不是延迟加载的,因为它被用在了format_html()中。是否有一种懒惰的format_html()方式?

版本:

  • Django 1.8
  • Pycharm 5.0.4

我不确定,但我怀疑 PyCharm 可能没有正确调用 Django 测试运行器。Django 需要执行一系列操作才能使测试正常运行,例如需要加载模型、解析 URL 模式等。因此,Django 使用管理命令 manage.py test 来运行测试。有可能 PyCharm 不知道这一点。 - Mad Wombat
如果你通过PyCharm运行开发服务器,你是否会得到完全相同的错误? - alecxe
你使用的是哪个PyCharm和Django版本?谢谢。 - alecxe
@alecxe,我在问题中添加了版本信息。 - guettli
4个回答

1

由于堆栈跟踪中存在url_patterns等内容,我认为DJANGO_SETTINGS_MODULE已正确设置。

但是,在运行任何其他内容之前,您仍需要调用django.setup()

import django
django.setup()

对我来说,这使得错误消息消失了。

0
如果上述所有方法都失败了,尝试在构造函数中初始化表单并进行导入:
    class ISUSearchForm(...):

    ...

    foo_filter = forms.Field()

    ...

    def __init__(*args, **kwargs)

      from ... import reverse_lazy
      from ... import FooFilter

      self.fields['foo_filter'] = \
        forms.ModelChoiceField(
          FooFilter.objects.all(),
          label=format_html(
            '<a href="%s">%s</a>',
            reverse_lazy('foo-filter'),
            FooFilter._meta.verbose_name
          )
        )

      super().__init__(*args, **kwargs)

错误可能是由不同的测试运行程序执行的确切import序列以及您仅使用类变量来自定义表单所导致的。

0

0

我自己在使用PyCharm时遇到了一些问题,我会假设你正在使用社区版(我也在使用)。

如果是这样的话,问题很可能是你没有正确地配置django。你可以尝试使用一些可能适用于此的技巧来解决这个问题。

我会从这里开始。(导入django以确保运行django控制台)

然后,也许这里会有帮助。

还有这个方法: 检查你运行的测试下的“编辑配置”,并将DJANGO_SETTINGS_MODULE=<应用程序名称>.settings添加到环境变量中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接