I am quite a newbbie with django-haystack. Following documentation and tutorials I was able to create a search App based on document content (DataBase SQLITE). As next step, I have updated my HTML template to request for more info (e.g.: Model, desired DB link, etc.)
I do not know how to approach this. Below the files I am using. My knowledge is still quite basic, so any help would be very much appreciated.
Thanks.
search.html
{% extends 'base.html' %}
{% block content %}
<h2> Doc Search Interface</h2>
<form method="get" action=".">
<table>
<P>Select Car Model:</P>
<P><LABEL ACCESSKEY=5><INPUT TYPE=checkbox NAME="CarModel" VALUE="A5"> A5</LABEL><BR>
<LABEL ACCESSKEY=8><INPUT TYPE=checkbox NAME="CarModel" VALUE="A8"> A8</LABEL><BR>
<LABEL ACCESSKEY=3><INPUT TYPE=checkbox NAME="CarModel" VALUE="A3"> A3</LABEL></P>
<tr>
<P>Type and click enter for Search!</P>
<input type="search" id="id_q" name="q" placeholder="Search" >
</tr>
</table>
{% if query %}
<h3>Let´s see if we have got here the document you were looking for...</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
{% endblock %}
models.py
from django.db import models
from django.contrib.auth.models import User
from django.template import RequestContext
from django.http import HttpResponse
class Document(models.Model):
user_id = models.CharField(max_length=6, default='admin')
pub_date = models.DateTimeField()
title = models.CharField(max_length=200)
Link= models.URLField()
content = models.TextField()
CarModel= models.TextField()
def get_absolute_url(self):
return self.Link
def __unicode__(self):
return self.title
search_indexes.py
from haystack import indexes
from test3.models import Document
class DocumentIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
content_auto=indexes.EdgeNgramField(model_attr='content')
CarModel=indexes.EdgeNgramField(model_attr='CarModel')
def get_model(self):
return Document
def index_queryset(self, using=None):
"""Used when the entire index for model is updated. Typically to avoid some results showing when admin do not want to"""
return self.get_model().objects
Following this other post Using django haystack search with global search bar in template I have updated the following files
urls.py
from django.conf.urls import url
from test3.views import MySearchView
# urls.py
urlpatterns = [url(r'^/search/?$', MySearchView.as_view(), name='My_search_view'),]
views.py
from haystack.forms import HighlightedSearchForm
from haystack.query import SearchQuerySet
from haystack.generic_views import SearchView
from haystack.views import search_view_factory
class MySearchView(SearchView):
"""My custom search view."""
def search_posts(request):
post_type = str(request.GET.get('CarModel')).lower()
print (str(request.GET.get('CarModel')).lower())
sqs = SearchQuerySet().filter(CarModel__contains=post_type)
clean_query = sqs.query.clean(post_type)
result = sqs.filter(content=clean_query)
view = search_view_factory(
view_class=SearchView,
template='search/search.html',
searchqueryset=result,
form_class=HighlightedSearchForm
)
return view(request)
However, I still did not make it work...Search results are still not filtered by 'Model' field. I guess I have to add extra code, but I do not know where...