问题
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
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/8577449/cant-use-django-management-commands-because-of-import-errors-when-haystack-trie