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

会有一股神秘感。 提交于 2019-12-10 22:08:31

问题


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


回答1:


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'),


来源:https://stackoverflow.com/questions/36958377/haystack-faceted-init-got-an-unexpected-keyword-argument-facet-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!