当 haystack 尝试导入多语言模型时,由于导入错误而无法使用 django 管理命令

发布于 2024-12-22 06:21:02 字数 456 浏览 3 评论 0原文

我正在使用 django-haystack 在我的网站上进行搜索。 我还在 I18n 中使用 django 多语言模型。 我在 search_indexes.py 中导入 MultilingualModel,

只要 INSTALLED_APPS 中没有 haystack,我就可以运行所有管理命令。

当 haystack 位于 INSTALLED_APPS 中并尝试运行syncdb或migrate(以及其他几个管理命令)时,我总是得到:

django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name MultilingualModel

I'm using django-haystack for searching on my site.
I'm also using django multilingual model for I18n.
I import MultilingualModel in search_indexes.py

I ca run all manangement commands as long as I don't have haystack in the INSTALLED_APPS.

When haystack is in the INSTALLED_APPS and try to run syncdb or migrate (and several other management commands) I'm always getting:

django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name MultilingualModel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

银河中√捞星星 2024-12-29 06:21:02

这可能与 haystack.autodiscover() 中进行的黑客攻击有关。此行为记录在此处: http ://docs.haystacksearch.org/dev/debugging.html#import-errors-on-start-up-mentioning-handle-registrations 这张票中有一个很长的讨论:https://github.com/toastdriven/django-haystack/issues/84

简而言之,将 haystack.autodiscover() 移动到您的 urls.py 有时可以解决此问题。在运行syncdb或migrate时设置HAYSTACK_ENABLE_REGISTRATIONS = False已经为我解决了这个问题,在我的settings.py中使用此代码段:

# FIXME: This is a complete hack to get around circular imports in 
# django-haystack and other apps such as django-endless-pagination
SKIP_COMMANDS = ['syncdb', 'migrate', 'schemamigration', 'datamigration']
if any([command in sys.argv for command in SKIP_COMMANDS]):
    HAYSTACK_ENABLE_REGISTRATIONS = False

This is likely related to the hacks done in haystack.autodiscover(). This behavior is documented here: http://docs.haystacksearch.org/dev/debugging.html#import-errors-on-start-up-mentioning-handle-registrations There is a long discussion in this ticket: https://github.com/toastdriven/django-haystack/issues/84

The long and short if it is that moving haystack.autodiscover() into your urls.py can sometimes resolve this issue. Setting HAYSTACK_ENABLE_REGISTRATIONS = False when running syncdb or migrate has resolved this for me using this snippet in my settings.py:

# FIXME: This is a complete hack to get around circular imports in 
# django-haystack and other apps such as django-endless-pagination
SKIP_COMMANDS = ['syncdb', 'migrate', 'schemamigration', 'datamigration']
if any([command in sys.argv for command in SKIP_COMMANDS]):
    HAYSTACK_ENABLE_REGISTRATIONS = False
朮生 2024-12-29 06:21:02

除非 haystack 位于 INSTALLED_APPS 中,否则 search_indexes.py 不会被处理。一般来说,问题在于 MultilingualModel 的导入。要么它没有真正安装在您的环境中(尝试从普通 python shell 导入它),要么您导入错误(例如,它实际上在另一个模块中)。

一旦您可以从 python shell 成功导入 MultilingualModel,您就不会有任何问题。

search_indexes.py doesn't get processed unless haystack is in INSTALLED_APPS. The problem is with the import of MultilingualModel in general. Either it's not truly installed in your environment (attempt to import it from a vanilla python shell), or you have the import wrong (it's actually in another module, for example).

Once you can successfully import MultilingualModel from a python shell, you won't have any problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文