版本:
django-haystack(2.6.1)
Django(1.11.4)
我正在尝试创建一个搜索表单,然后是tourial:
过滤开始日期后,sqs变空
怎么了?
forms.py:
class DateRangeSearchForm(SearchForm):
start_date = forms.DateTimeField(required=False)
end_date = forms.DateTimeField(required=False)
def search(self):
sqs = super(DateRangeSearchForm, self).search()
print (sqs[0].object.publish_date) #2017-10-23 02:10:40.673000+00:00
print (self.cleaned_data['start_date']) #2017-10-17 00:00:00+08:00
print (self.cleaned_data['start_date']<sqs[0].object.publish_date) #true
print (len(sqs)) #19
if not self.is_valid():
print("no query found/n")
return self.no_query_found()
if self.cleaned_data['start_date']:
sqs = sqs.filter(publish_date__gte=self.cleaned_data['start_date'])
print(len(sqs))#0
return sqs
views.py:
from .forms import DateRangeSearchForm
from .models import Article
class postManageSearchView(SearchView):
template_name='backendSys/post_manageSearch.html'
form_class = DateRangeSearchForm
def get_context_data(self, **kwargs):
context = super(postManageSearchView, self).get_context_data(**kwargs)
admin = self.request.user.profile.admin.get(admin__user__pk=self.kwargs['pk'])
permission = admin.permission.filter()
articles = Article.objects.filter()
context['permission'] = permission
context['articles'] = articles
return context
答案 0 :(得分:0)
尝试在forms.py
from haystack.query import SearchQuerySet
class DateRangeSearchForm(SearchForm):
start_date = forms.DateTimeField(required=False)
end_date = forms.DateTimeField(required=False)
def search(self):
sqs = super(DateRangeSearchForm, self).search()
print (sqs[0].object.publish_date)
print (self.cleaned_data['start_date'])
print (self.cleaned_data['start_date']<sqs[0].object.publish_date)
print (len(sqs))
if not self.is_valid():
print("no query found/n")
return self.no_query_found()
if self.cleaned_data['start_date']:
sqs = SearchQuerySet().filter(publish_date__gte=self.cleaned_data['start_date'])
print(len(sqs))
return sqs
在get_queryset()
views.py
class postManageSearchView(SearchView):
template_name='backendSys/post_manageSearch.html'
form_class = DateRangeSearchForm
def get_context_data(self, **kwargs):
context = super(postManageSearchView, self).get_context_data(**kwargs)
admin = self.request.user.profile.admin.get(admin__user__pk=self.kwargs['pk'])
permission = admin.permission.filter()
articles = Article.objects.filter()
context['permission'] = permission
context['articles'] = articles
return context
def get_queryset(self):
queryset = super(postManageSearchView, self).get_queryset()
return queryset.all