divio / aldryn-search

Haystack 2.0 search index for django CMS
Other
48 stars 77 forks source link

AttributeError in base.py: "_backend_alias" #75

Closed wfehr closed 5 years ago

wfehr commented 7 years ago

I use djangocms-blog and the search-function that comes with it (more or less).

When I set

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

I get an AttributeError in aldryn_search/base.py line 37 when I want to add a new blog-post:

AttributeError: 'PostIndex' object has no attribute '_backend_alias'

A simple solution would be to check if _backend_alias is set and set it to None if its not (or DEFAULT_ALIAS from haystack).

from haystack.constants import DEFAULT_ALIAS
...
def prepare(self, obj):
    if not hasattr(self, '_backend_alias'):
        self._backend_alias = DEFAULT_ALIAS # or set to None
        ...

Would this be possible to implement in an upcoming version or do you think the error is somewhere completely else?

Here is a part of the traceback I get:

  File "/usr/local/lib/python3.6/site-packages/djangocms_blog/models.py", line 255, in save
    super(Post, self).save(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/parler/models.py", line 657, in save
    super(TranslatableModelMixin, self).save(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 833, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "/usr/local/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 191, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python3.6/site-packages/haystack/signals.py", line 52, in handle_save
    index.update_object(instance, using=using)
  File "/usr/local/lib/python3.6/site-packages/haystack/indexes.py", line 284, in update_object
    backend.update(self, [instance])
  File "/usr/local/lib/python3.6/site-packages/haystack/backends/elasticsearch_backend.py", line 168, in update
    prepped_data = index.full_prepare(obj)
  File "/usr/local/lib/python3.6/site-packages/haystack/indexes.py", line 208, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "/usr/local/lib/python3.6/site-packages/aldryn_search/base.py", line 37, in prepare
    current_language = self.get_current_language(using=self._backend_alias, obj=obj)
AttributeError: 'PostIndex' object has no attribute '_backend_alias'
czpython commented 7 years ago

Hi there, Thanks for reporting this. The issue happens because django-haystack >= 2.6 has deprecated the _get_backend method which we're currently relying on to set this hidden attribute.

melbic commented 5 years ago

Could you please merge the fix?

sidneywidmer commented 5 years ago

Any update on this? A new release with this fix included would be nice 👍