Can't use django management commands because o

2019-05-30 09:27发布

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

2条回答
唯我独甜
2楼-- · 2019-05-30 10:07

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.

查看更多
唯我独甜
3楼-- · 2019-05-30 10:10

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
查看更多
登录 后发表回答