Sto cercando di fare un modulo di ricerca personalizzato utilizzando pagliaio Django, ho appena modifico dalla documentazione del pagliaio:django pagliaio modulo personalizzato
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
a:
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'),
)
quando vado al URL, si dice: AttributeError a/ricerca/
oggetto 'CustomSearchForm' non ha alcun attributo 'cleaned_data'
può voi ragazzi mi può aiutare? thx
Quindi provo a commentare il metodo di ricerca, ma quando invio una parola nel campo personalizzato, il risultato è sempre nulla, solo quando invio una parola a un campo non personalizzato può darmi il risultato che voglio, ho già provato a capirlo tutto il giorno, pls help
Ecco un modo migliore, 'if isinstance (sqs, EmptySearchQuerySet): restituisce sqs' fallo dopo la chiamata' super() 'e rimuove tutto' is_valid() ' – nitely