Cadilac
Cadilac

Reputation: 1100

django-haystack - No module named search_sites

I download django-haystack-1.1.0.tar.gz, unzip it, then copy haystack directory, which is in it, to my apps directory and add haystack to my INSTALLED_APPS (also add whoosh, because i copy it too), but when i restart server i get 500 internal error. Then i delete, for experiment, handle_registrations() from haystack.__init__ and site start working, but when i try search by haystack i get No fields were found in any search_indexes. Please correct this before attempting to search. In settings.py i have also:

HAYSTACK_SITECONF = 'search_sites'  
HAYSTACK_SEARCH_ENGINE = 'whoosh'  
HAYSTACK_WHOOSH_PATH = os.path.join(PROJECT_ROOT, 'mysite_search_sites')

Then i undelete handle_registrations(), delete haystack from INSTALLED_APPS and restart server and now i am getting No module named search_sites.
Also import haystack and haystack.__version__ works but haystack.management.commands didn't.
Could someone help me with this, please?

EDIT
My traceback:

/lib/python2.7/django/core/handlers/base.py in get_response
                        response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/myproject/apps/djangobb_forum/util.py in wrapper
            output = function(request, *args, **kwargs)
 ...
▶ Local vars
/myproject/apps/djangobb_forum/util.py in wrapper
            result = func(request, *args, **kwargs)
 ...
▶ Local vars
/myproject/apps/djangobb_forum/views.py in search
                for post in posts:
 ...
▶ Local vars
/myproject/apps/haystack/query.py in _manual_iter
            if not self._fill_cache(current_position, current_position + ITERATOR_LOAD_PER_QUERY):
 ...
▶ Local vars
/myproject/apps/haystack/query.py in _fill_cache
        results = self.query.get_results()
 ...
▶ Local vars
/myproject/apps/haystack/backends/__init__.py in get_results
                self.run()
 ...
▶ Local vars
/myproject/apps/haystack/backends/__init__.py in run
        results = self.backend.search(final_query, **kwargs)
 ...
▶ Local vars
/myproject/apps/haystack/backends/__init__.py in wrapper
            return func(obj, query_string, *args, **kwargs)
 ...
▶ Local vars
/myproject/apps/haystack/backends/whoosh_backend.py in search
            self.setup()
 ...
▶ Local vars
/myproject/apps/haystack/backends/whoosh_backend.py in setup
        self.content_field_name, self.schema = self.build_schema(self.site.all_searchfields())
 ...
▶ Local vars
/myproject/apps/haystack/backends/whoosh_backend.py in build_schema
            raise SearchBackendError("No fields were found in any search_indexes. Please correct this before attempting to search.")
 ...
▶ Local vars

Upvotes: 0

Views: 2876

Answers (2)

Rakesh
Rakesh

Reputation: 82765

You need to create search_sites.py in ur project root dir according to ur settings.py and add

import haystack
haystack.autodiscover()

This will fix the "No module named search_sites" error And this is the LatestDocs for Django-Haystack Configurations

Upvotes: 2

Shaun O'Keefe
Shaun O'Keefe

Reputation: 1338

From the install steps you've listed it sounds like you're missing a few steps.

Definitely revisit the Haystack setup instructions with a particular eye to looking at the Create a Search Site and Creating Indexes sections.

The long and short is you seem to be missing an indexes file. Haystack registers a bunch of stuff from your indexes when it is first included, so that explains why you're getting errors from haystack.__init__

Add a file called 'search_indexes.py' to your application directory. This file contains a list of the indexes you want to generate for different models. A simple example would be:

from haystack.indexes import *
from haystack import site
from myapp.models import MyModel

class MyModelIndex(SearchIndex):
    text = CharField(document=True, use_template=True)

    def prepare(self, obj):
        self.prepared_data = super(MyModelIndex, self).prepare(obj)
        self.prepared_data['text'] = obj.my_field

site.register(MyModel, MyModelIndex)

This will add a free text search field called 'text' to your index. When you search for free text without a field to search specified, haystack will search this field by default. The property my_field from the model MyModel is added to this text field and is made searchable. This could, for example, be the name of the model or some appropriate text field. The example is a little naive, but it will do for now to help you get something up and running, and you can then read up a bit and expand it.

The call site.register registers this index against the model MyModel so haystack can discover it.

You'll also need a file called search_sites.py (Name as per your settings) in your project directory to point to the index files you've just made. Adding the following will make it look through your apps and auto-discover any indexes you've registered.

import haystack
haystack.autodiscover()

Upvotes: 2

Related Questions