gpt4 book ai didi

django-haystack - django haystack 自定义表单

转载 作者:行者123 更新时间:2023-12-04 07:04:52 26 4
gpt4 key购买 nike

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

表格.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

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

当我转到 url 时,它说:AttributeError at/search/

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

你们能帮帮我吗?谢谢

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

最佳答案

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

沿着这些路线的东西:

...
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 and'
这为我摆脱了错误。希望这可以帮助。

所以,
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 的相对初学者,但它对我有用。

关于django-haystack - django haystack 自定义表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11466792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com