[Django]-Django-haystack result filtering using attributes?

6👍

Have you looked at the faceting tutorial/documentation on the django-haystack website? It walks you through an example of filtering based on author of the Note model introduced in the getting started tutorial.

Another option separate from haystack and searching is django-filter by Alex Gaynor, it allows you to filter fields based on the contents of the model and not on an index. Therefore it can be used on models that aren’t indexed with django-haystack. You can check out the repository here. There are good docs in the doc folder and the included tests show off all the functionality.

👤Rigsby

0👍

if we summarize how to add haystack faceting

  1. you need to add faceted fields to your index model
 title =  CharField(model_attr='title', faceted=True)
 description =  CharField(model_attr='description', faceted=True)
  1. change your queryset to
  sqs = SearchQuerySet().facet('title').facet('description')
  1. use faceted versions of search form and view that haystack provides in urls.py use this
  url(r'^$', FacetedSearchView(form_class=FacetedSearchForm, searchqueryset=sqs), name='haystack_search'),
  1. add faceting part to your template like this http://docs.haystacksearch.org/dev/faceting.html#display-the-facets-in-the-template

  2. rebuild your index to see effects of faceting

 
   python manage.py rebuild_index

Leave a comment