Django haystack autocomplete

I am trying to use django-haystack (it was about 2 days) and I have a basic Getting Started example and it looks very impressive. Now I would like to try the autocomplete function on a haystack.

http://readthedocs.org/docs/django-haystack/en/v1.2.4/autocomplete.html

The first part seems beautiful: "Setting up the data" seems quite simple. However, I'm not sure where to write "Executing the request": ie in which view should I include:

from haystack.query import SearchQuerySet sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', '')) 

My current urls.py is simple and configured as follows:

 urlpatterns = patterns('', # Examples: ('^hello/$', hello), (r'^$', hello), (r'^search/', include('haystack.urls')), # url(r'^$', 'mysite.views.home', name='home'), # url(r'^mysite/', include('mysite.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: #url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), ) 

Looking at the following blog:

http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende

I would like to have something like:

 url(r'^search/', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'), 

but where should sqs be specified? in urls.py or views.py? I tried both, but they give me a "query" error, which is not found in the sqs statement.

+7
source share
3 answers

Usually this will be in your own haystack urls, haystack.urls in your current urls.py points to the default urls. Create a haystack_urls.py file and add it in your urls.py, for example.

 url(r'^search/', include('yourproject.haystack_urls')), 

in this file you can add your own code, for example.

 from haystack.views import SearchView from haystack.query import SearchQuerySet sqs = SearchQuerySet() # edit remove line that was incorret urlpatterns = patterns('haystack.views', url(r'^&', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'), ) 

to wrap the view for the request, try something like

 class SearchWithRequest(SearchView): __name__ = 'SearchWithRequest' def build_form(self, form_kwargs=None): if form_kwargs is None: form_kwargs = {} if self.searchqueryset is None: sqs = SearchQuerySet().filter(content_auto=self.request.GET.get('q', '')) form_kwargs['searchqueryset'] = sqs return super(SearchWithRequest, self).build_form(form_kwargs) 
+4
source

JamesO's solution did not work for me, so I defined a custom form class that actually overrides only the search method. So, in general, you need to try

  • On your urls.py use FacetedSearchView insted from basic_search or SearchView .
  • Create your own form class by overriding the search method:

     class FacetedSearchFormWithAuto(FacetedSearchForm): def search(self): if not self.is_valid(): return self.no_query_found() if not self.cleaned_data.get('q'): return self.no_query_found() sqs = self.searchqueryset.autocomplete(content_auto=self.cleaned_data['q']) if self.load_all: sqs = sqs.load_all() for facet in self.selected_facets: if ":" not in facet: continue field, value = facet.split(":", 1) if value: sqs = sqs.narrow(u'%s:"%s"' % (field, sqs.query.clean(value))) return sqs 
  • specify form_class in FacetedSearchView form_class=FacetedSearchFormWithAuto

+1
source

If you need help with autocomplete, I think it will be good for you, this is a complete solution Full-text search in Django-haystack, but there are no edges

+1
source

All Articles