pip install django-haystack
python
INSTALLED_APPS = [
...
'haystack',
]
HAYSTACK_CONNECTIONS = {
'default': {
},
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
python
from haystack import indexes
from .models import Book
class BookIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return Book
def index_queryset(self, using=None):
return self.get_model().objects.all()
python manage.py rebuild_index
python
from haystack.query import SearchQuerySet
def search_books(request):
query = request.GET.get('q', '')
results = SearchQuerySet().filter(content=query)
return render(request, 'search_results.html', {'results': results})