2017-04-30 4 views
0

내 Django Haystack Whoosh 검색에서 찾은 결과가 없으면 모든 결과를 반환 할 것을 목표로합니다.Django 건초 더미에서 SearchForm 메소드를 오버라이드하는 방법

Haystack 설명서에서이 작업을 수행하기 위해 SearchForm의 no_query_found 메서드 (아래 참조)를 재정의 할 수 있다고 읽었습니다. 그러나 나는 어떻게 될지 모른다. 어떤 아이디어? 여기

class SearchForm(forms.Form): 
    def no_query_found(self): 

    """ 
    Determines the behavior when no query was found. 
    By default, no results are returned (``EmptySearchQuerySet``). 
    Should you want to show all results, override this method in your 
    own ``SearchForm`` subclass and do ``return self.searchqueryset.all()``. 
    """ 

    return EmptySearchQuerySet() 

내 forms.py입니다 :

from django import forms 
from .models import Blog, Category 
from locations.models import Country, County, Municipality, Village 
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() 

    if not self.is_valid(): 
     return self.no_query_found() 

    # 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 

답변

0

음 ... 단지

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

    def no_query_found(self): 
     # do here your custom stuff, you get access to self.searchqueryset 
     # and return it 

    def search(self): 
     # your search method 
+0

고맙습니다. 생각 해봤지만 혼란스러워하는 다른 일을하는 것으로 나타났습니다. –

0

그냥 clearify하기 위해 서브 클래스에 메소드를 추가,이게 내가이었다 의도 된 결과를 얻었다 를 목표로하여. Muj 고마워!

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

def no_query_found(self): 
    # Added the return clause here: 
    return self.searchqueryset.all()