1

我正在使用 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

有谁知道可能是什么问题?

4

1 回答 1

0

与此同时,我切换到了 ElasticSearch 后端,但问题仍然存在,这表明它可能是 haystack 中的问题,而不是 Whoosh 中的问题。

问题是 python 值TrueFalse没有保存为布尔值,而是作为字符串,并且它们没有转换回布尔值。要过滤布尔值,您必须检查字符串'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
于 2014-06-14T16:45:27.547 回答