django haystack自定义表单

时间:2012-07-13 08:26:21

标签: django-haystack

我正在尝试使用django haystack创建自定义搜索表单,我只是从haystack的文档中修改:

forms.py

from django import forms
from haystack.forms import SearchForm

class DateRangeSearchForm(SearchForm):
    start_date = forms.DateField(required=False)
    end_date = forms.DateField(required=False)

   def search(self):
        # First, store the SearchQuerySet received from other processing.
        sqs = super(DateRangeSearchForm, self).search()

        # Check to see if a start_date was chosen.
        if self.cleaned_data['start_date']:
            sqs = sqs.filter(pub_date__gte=self.cleaned_data['start_date'])

        # Check to see if an end_date was chosen.
        if self.cleaned_data['end_date']:
            sqs = sqs.filter(pub_date__lte=self.cleaned_data['end_date'])

        return sqs

到:

from django import forms
from haystack.forms import HighlightedModelSearchForm

class CustomSearchForm(HighlightedModelSearchForm):
    title   = forms.CharField(max_length = 100, required = False)
    content = forms.CharField(max_length = 100, required = False)
    date_added = forms.DateField(required = False)
    post_by = forms.CharField(max_length = 100, required = False)

    def search(self):
        sqs = super(CustomSearchForm, self).search()
        if self.cleaned_data['post_by']:
            sqs = sqs.filter(content = self.cleaned_data['post_by'])
        if self.cleaned_data['title']:
            sqs = sqs.filter(content = self.cleaned_data['title'])
        if self.cleaned_data['content']:
            sqs = sqs.filter(content = self.cleaned_data['content'])
        if self.cleaned_data['date_added']:
            sqs = sqs.filter(content = self.cleaned_data['date_added']) 
        return sqs

haystack .urls:

urlpatterns = patterns('haystack.views',
    url(r'^$', search_view_factory(view_class = SearchView, form_class = CustomSearchForm), name='haystack_search'),
)

当我转到网址时,它会说:/ error /

中的AttributeError

'CustomSearchForm'对象没有属性'cleaning_data'

你能帮助我吗? THX

然后我尝试评论搜索方法,但是当我在自定义字段中提交一个单词时,结果总是一无所获,只有当我向非自定义字段提交单词时,它才能给我想要的结果,已经尝试过了要整天理解这一点,请帮助

2 个答案:

答案 0 :(得分:9)

我知道这是一个有点老问题,但是为了帮助那些可能正在查看这个并想知道同样事情的人,这就是我在相同情况下工作的方式。

这些方面的东西:

...
def search(self)
    sqs=super(MyFooSearchForm, self).search()

    if self.is_valid() and self.cleaned_data['foo']:
        sqs = sqs.filter(foostuff__exact=self.cleaned_data['foo'])

    return sqs

基本上,我在self.cleaned_data ['']之前添加了'self.is_valid和' 这摆脱了我的错误。希望这会有所帮助。

所以,

def search(self):
    sqs = super(CustomSearchForm, self).search()
    if self.cleaned_data['post_by']:
        sqs = sqs.filter(content = self.cleaned_data['post_by'])
    if self.cleaned_data['title']:
        sqs = sqs.filter(content = self.cleaned_data['title'])
    if self.cleaned_data['content']:
        sqs = sqs.filter(content = self.cleaned_data['content'])
    if self.cleaned_data['date_added']:
        sqs = sqs.filter(content = self.cleaned_data['date_added']) 
    return sqs

会变成:

def search(self):
    sqs = super(CustomSearchForm, self).search()
    if self.is_valid() and self.cleaned_data['post_by']:
        sqs = sqs.filter(content = self.cleaned_data['post_by'])
    if self.is_valid() and self.cleaned_data['title']:
        sqs = sqs.filter(content = self.cleaned_data['title'])
    if self.is_valid() and self.cleaned_data['content']:
        sqs = sqs.filter(content = self.cleaned_data['content'])
    if self.is_valid() and self.cleaned_data['date_added']:
        sqs = sqs.filter(content = self.cleaned_data['date_added']) 
    return sqs

可能有更好的方法可以做到这一点,而且我是django / python的相对初学者,但它对我有用。

答案 1 :(得分:0)

首先要调用form.is_valid()。在调用isvalid()方法之前,数据不会处于清除状态。因此,在验证所有字段时,如果所有字段都按照表单定义有效,则会将所有字段逐一放入其cleaned_data属性中。如果任何字段使is_valid()为false,那么cleaned_data也将存在并包含先前验证过的数据。 >