Search code examples
pythondjangodjango-haystackfaceted-search

Haystack Faceted: __init__() got an unexpected keyword argument 'facet_fields'


While enjoying my first results with haystack 2.4.1 (Django 1.8), I have to admit that I'm having a hard time on learning it. The documentation is sometimes incomplete, and some features have to few examples.

Faceted search is one of them.

I'm following the documentation, and at the url.py:

urlpatterns = patterns('haystack.views',
    url(r'^$', FacetedSearchView(form_class=FacetedSearchForm, facet_fields=['author']), name='haystack_search'),
)

I'm getting the following error:

TypeError at /tag_analytics/faceted_search/

__init__() got an unexpected keyword argument 'facet_fields'

Looks like the FacetSearchView is not accepting the facet_fields argument, which took me to version 2.4.0, when the right way to do this was

FacetedSearchView(form_class=FacetedSearchForm, searchqueryset=sqs)

Although I'm sure my version is 2.4.1, I tried this option, and got a

TypeError at /tag_analytics/faceted_search/

slice indices must be integers or None or have an __index__ method

Thanks in advance for any clues!

the best, alan


Solution

  • The documentation is just wrong, and confusing. You cannot pass facet_fields to the constructor for FacetedSearchView.

    The approach you have taken is correct although rather than put all those arguments in the url definition, you should create your own view - something like this:

    # tag_analytics/views.py
    from haystack.generic_views import FacetedSearchView as BaseFacetedSearchView
    
    # Now create your own that subclasses the base view
    class FacetedSearchView(BaseFacetedSearchView):
        form_class = FacetedSearchForm
        facet_fields = ['author']
        template_name = 'search.html'
        context_object_name = 'page_object'
    
        # ... Any other custom methods etc
    

    Then in urls.py:

    from tag_analytics.views import FacetedSearchView
    #...
    url(r'^$', FacetedSearchView.as_view(), name='haystack_search'),