我正在使用Django Haystack v2.0.0和Whoosh v2.4.0。根据Haystack的documentation搜索索引,可以在model_attr
参数中使用Django的相关字段查找。但是,使用manage.py shell命令运行以下代码:
from haystack.query import SearchQuerySet
for r in SearchQuerySet():
print r.recruitment_agency # Prints True for every job
print r.recruitment_agency == r.object.employer.recruitment_agency
# Prints False if r.object.employer.recruitment_agency is False
我尝试过多次重建索引,索引的目录是可写的,我没有收到任何错误消息。所有其他字段按预期工作。
我有以下(简化)模型:
公司/ models.py:
class Company(models.Model):
recruitment_agency = models.BooleanField(default=False)
作业/ models.py:
class Job(models.Model):
employer = models.ForeignKey(Company, related_name='jobs')
作业/ search_indexes.py:
class JobIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
recruitment_agency = indexes.BooleanField(model_attr='employer__recruitment_agency')
def get_model(self):
return Job
作业/ forms.py:
class JobSearchForm(SearchForm):
no_recruitment_agencies = forms.BooleanField(label="Hide recruitment agencies", required=False)
def search(self):
sqs = super(JobSearchForm, self).search()
if self.cleaned_data['no_recruitment_agencies']:
sqs = sqs.filter(recruitment_agency=False)
return sqs
有谁知道可能是什么问题?
答案 0 :(得分:0)
与此同时,我已切换到ElasticSearch后端,但问题仍然存在,表明它可能是大海捞针中的一个问题,而不是在Whoosh中。
问题是python值True
和False
不是保存为布尔值,而是保存为字符串,并且它们不会转换回布尔值。要过滤布尔值,您必须检查字符串'true'
和'false'
:
class JobSearchForm(SearchForm):
no_recruitment_agencies = forms.BooleanField(label="Hide recruitment agencies", required=False)
def search(self):
sqs = super(JobSearchForm, self).search()
if self.cleaned_data['no_recruitment_agencies']:
sqs = sqs.filter(recruitment_agency='false') # Change the filter here
return sqs