pip install django-haystack
python
INSTALLED_APPS = [
...
'haystack',
]
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_cn_backend.WhooshEngine',
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
},
}
python
from haystack import indexes
from myapp.models import MyModel
class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return MyModel
def index_queryset(self, using=None):
return self.get_model().objects.all()
{{ object.field_name }}
python manage.py rebuild_index
python
from haystack.views import SearchView
class MySearchView(SearchView):
template_name = 'search.html'