2016-04-30 84 views
2

在用Haystack 2.4.1(Django 1.8)享受我的第一個結果的同時,我不得不承認我很難學習它。這些文檔有時是不完整的,有些功能只有幾個例子。Haystack Faceted:__init __()得到了一個意想不到的關鍵字參數'facet_fields'

分面搜索就是其中之一。

我正在關注的documentation,並在url.py:

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

,我發現了以下錯誤:

TypeError at /tag_analytics/faceted_search/

__init__() got an unexpected keyword argument 'facet_fields'

貌似FacetSearchView不接受facet_fields參數,這把我帶到2.4.0版本,當正確的方式做到這一點是

FacetedSearchView(form_class=FacetedSearchForm, searchqueryset=sqs) 

雖然我敢肯定,我的版本是2.4.1,我試過這個選項,並提前獲得了

TypeError at /tag_analytics/faceted_search/

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

感謝任何線索!

最好, 艾倫

+1

我有一個「解決方案」,我懷疑是正確的,因爲這意味着文檔是完全錯誤的。無論如何,在url.py我用'FacetedSearchView.as_view(form_class)'從'haystack.views'導入FacetedSearchView'從'haystack.generic_views導入FacetedSearchView'和'FacetedSearchView(form_class = FacetedSearchForm,facet_fields = ['author']) = FacetedSearchForm,facet_fields = ['author'],template_name ='search.html',context_object_name ='page_object')'。它有效,但我仍然想要了解問題所在! –

回答

7

的文件是錯誤的,和混亂。您無法將facet_fields傳遞給FacetedSearchView的構造函數。

你所採取的做法是正確的,雖然,而不是把所有這些參數在url定義,您應該創建自己的觀點 - 是這樣的:

# 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 

然後在urls.py

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

感謝您的解釋!只是糾正了一個小錯字,現在它的工作很好! –

+0

@AlanTygel我得到一個錯誤:名稱'FacetedSearchForm'沒有與這個答案定義。如果使用urls.py變體沒有錯誤,但也沒有結果,不知道爲什麼 –

+0

@VicNicethemer你必須導入它:'從haystack.forms導入FacetedSearchForm'。這是在OP的問題中假設的。 – solarissmoke

相關問題