【发布时间】:2016-07-13 09:32:28
【问题描述】:
我的模型具有这样的属性:
new = models.BooleanField(default=False, verbose_name=_("Is new"),
help_text=_("By default product is marked as new by {0} days since creation.")
.format(settings.PRODUCT_IS_NEW_EXPIRATION_DAYS))
不幸的是,这适用于 runserver 命令,但是当我想运行测试时它失败并且我收到以下错误:
django.core.exceptions.AppRegistryNotReady:在应用程序注册表准备好之前无法初始化翻译基础结构。检查您在导入时没有进行非惰性 gettext 调用。
这个错误很明显。我正在尝试在应用准备好之前使用翻译。
是否有可能在导入时需要字符串格式的翻译?
编辑: 追溯:
File "./manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "django/core/management/__init__.py", line 328, in execute
django.setup()
File "django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "django/apps/config.py", line 86, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "haystack/__init__.py", line 57, in <module>
signal_processor_class = loading.import_class(signal_processor_path)
File "haystack/utils/loading.py", line 32, in import_class
module_itself = importlib.import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "utils/signals.py", line 12, in <module>
from products.models import Products
File "products/models/__init__.py", line 2, in <module>
from .carousel import ProductCarousel, ProductCarouselImage
File "products/models/carousel.py", line 10, in <module>
from products.models.product import Products
File "products/models/product.py", line 210, in <module>
class Products(models.Model):
File "products/models/product.py", line 241, in Products
help_text=(ugettext("By default product is marked as new by {0} days since creation.")).format(
File "django/utils/translation/__init__.py", line 84, in ugettext
return _trans.ugettext(message)
File "django/utils/translation/trans_real.py", line 330, in ugettext
return do_translate(message, 'ugettext')
File "django/utils/translation/trans_real.py", line 307, in do_translate
_default = _default or translation(settings.LANGUAGE_CODE)
File "django/utils/translation/trans_real.py", line 209, in translation
_translations[language] = DjangoTranslation(language)
File "django/utils/translation/trans_real.py", line 118, in __init__
self._add_installed_apps_translations()
File "django/utils/translation/trans_real.py", line 159, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
【问题讨论】:
-
这是使用
ugettext还是ugettext_lazy? -
ugettext或ugettext_lazy没有区别。同样的错误。 -
我希望惰性代理对象的格式能够处理这个问题。我已经编辑了你的问题,希望它更清楚,如果你觉得这不正确,请随时回滚
-
出于好奇,
_("By default product is marked as new by %d days since creation.") % settings.PRODUCT_IS_NEW_EXPIRATION_DAYS有效吗?
标签: django django-i18n