I am trying to use django-haystack (been around 2 days now), and I have got the basic Getting Started example working and it looks very impressive. I would now like to try the autocomplete function on haystack.
http://readthedocs.org/docs/django-haystack/en/v1.2.4/autocomplete.html
The first part seems fine: "Setting up the data" seems simple enough. However, I am not sure where the "Performing the Query" needs to be written: i.e 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 set up 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 the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.
Usually this would be in your own haystack urls, haystack.urls
in your current urls.py is pointing to the default urls. Create a file haystack_urls.py and in your urls.py add it e.g.
url(r'^search/', include('yourproject.haystack_urls')),
in that file you can then add your custom code e.g.
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 a view for 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)
The solution of JamesO was not working for me, so i defined a custom form class, which is actually overrides the 'search' method only. So, in general, you've got to try
FacetedSearchView
insted of basic_search
or SearchView
.Create your own form class, 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
If you need help with autocomplete, I think this would be good for you, it's complete solution Django-haystack full text search working but facets don't
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With