django haystack定制表单 [英] django haystack custom form

查看:186
本文介绍了django haystack定制表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用django haystack制作自定义搜索表单,我只是从haystack的文档中进行了修改:

I'm trying to make a custom search form using django haystack, i just modify from haystack's documentation :

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:

haystack .urls :

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

转到网址,它说:/ search /

when i go to the url, it says : AttributeError at /search/

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

'CustomSearchForm' object has no attribute 'cleaned_data'

你们能帮我吗? thx

can you guys help me? thx

然后我尝试注释搜索方法,但是当我在自定义字段中提交单词时,结果始终为空,仅当我向non提交单词时-custom字段可以给我想要的结果,已经整天试图理解这一点,请帮助

Then i try to comment the search method, but when i submit a word into the custom field, the result is always nothing, only when i submit a word to non-custom field it can gimme the result i want, already tried to understand this all day long, pls help

推荐答案

这个问题有点老了,但是为了帮助其他可能正在查看此问题并想知道同一件事的人,这就是我如何在相同情况下使用它。

I know this is a bit old question, but to help others who may be viewing this and wondering the same thing, this is how I got it working in the same situation.

这些行:

...
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.is_valid和' self.cleaned_data ['']
这为我摆脱了错误。希望这会有所帮助。

Basically, I added 'self.is_valid and' before self.cleaned_data[''] this got rid of the error for me. Hope this helps.

所以,

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的相对入门者,但对我有用

There may be a better way to do this, and I'm a relative beginner for django/python but it worked for me.

这篇关于django haystack定制表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆