我使用的是python 3.5.2,Django 1.9和django-haystack 2.5.1。
我有一些模特:
class Annotation(TimeStampedModel):
... other attributes ...
entity = models.ForeignKey('another_app.Entity', blank=True, null=True)
class Entity(models.Model):
... other attributes ...
我使用此配置来覆盖HAYSTACK_CONNECTIONS设置变量,另一个(TEST_INDEX)将所有测试重定向到不同的搜索索引。
@override_settings(HAYSTACK_CONNECTIONS=TEST_INDEX, HAYSTACK_SIGNAL_PROCESSOR='haystack.signals.BaseSignalProcessor')
class SearchEnpointsTests(OurAPITestCase):
fixtures = ['test_data.json']
@classmethod
def setUpClass(cls):
super(SearchEnpointsTests, cls).setUpClass()
haystack.connections.reload('default')
call_command('update_index', interactive=False, verbosity=0)
@classmethod
def tearDownClass(cls):
return super(SearchEnpointsTests, cls).tearDownClass()
# call_command('clear_index', interactive=False, verbosity=0)
test_search(self):
...
并且,作为设置变量:
# haystack:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': ELASTICSEARCH_HOST,
'INDEX_NAME': 'haystack'
}
}
TEST_INDEX = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': ELASTICSEARCH_HOST,
'INDEX_NAME': 'test_index',
},
}
在配置haystack实时之前,它已经工作了,现在我用这个改变了配置:
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
它在测试过程中引发了异常:
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 160, in __get__
rel_obj = getattr(instance, self.cache_name)
AttributeError: 'Annotation' object has no attribute '_entity_cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/project_folder/.venv/lib/python3.5/site-packages/django/test/testcases.py", line 1036, in setUpClass
'database': db_name,
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 119, in call_command
return command.execute(*args, **defaults)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 100, in loaddata
self.load_label(fixture_label)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 158, in load_label
obj.save(using=self.using)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/serializers/base.py", line 201, in save
models.Model.save_base(self.object, using=using, raw=True, **kwargs)
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/base.py", line 745, in save_base
update_fields=update_fields, raw=raw, using=using)
File "/project_folder/.venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 192, in send
response = receiver(signal=self, sender=sender, **named)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/signals.py", line 52, in handle_save
index.update_object(instance, using=using)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/indexes.py", line 278, in update_object
backend.update(self, [instance])
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/backends/elasticsearch_backend.py", line 168, in update
prepped_data = index.full_prepare(obj)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/indexes.py", line 208, in full_prepare
self.prepared_data = self.prepare(obj)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/indexes.py", line 199, in prepare
self.prepared_data[field.index_fieldname] = field.prepare(obj)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/fields.py", line 205, in prepare
return self.convert(super(CharField, self).prepare(obj))
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/fields.py", line 88, in prepare
values = self.resolve_attributes_lookup(current_objects, attrs)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/fields.py", line 108, in resolve_attributes_lookup
if not hasattr(current_object, attributes[0]):
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 169, in __get__
rel_obj = qs.get()
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/query.py", line 387, in get
self.model._meta.object_name
apps.another_app.models.DoesNotExist: Problem installing fixture '/project_folder/fixtures/test_data.json': Entity matching query does not exist.