我正在使用 Django Haystack v2.0.0 和 Whoosh v2.4.0。根据 Haystack 的文档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
我已经尝试过多次重建索引,索引的目录是可写的,并且我没有收到任何错误消息。所有其他字段都按预期工作。
我有以下(简化的)模型:
company/models.py:
class Company(models.Model):
recruitment_agency = models.BooleanField(default=False)
工作/模型.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
有谁知道可能是什么问题?