Domanda

I'm using Django Haystack v2.0.0 and Whoosh v2.4.0. According to Haystack's documentation search indexes can use Django's related field lookup in the model_attr parameter. However, running the following code using manage.py shell command:

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

I have tried rebuilding the index several times, the index's directory is writeable, and I don't get any error messages. All other fields work as expected.

I have the following (simplified) models:
companies/models.py:

class Company(models.Model):
    recruitment_agency = models.BooleanField(default=False)

jobs/models.py:

class Job(models.Model):
    employer = models.ForeignKey(Company, related_name='jobs')

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

jobs/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

Does anyone know what could be the problem?

È stato utile?

Soluzione

Meanwhile I've switched over to the ElasticSearch backend, but the problem persisted, indicating that it might be a problem in haystack, and not in Whoosh.

The problem is that the python values True and False are not saved as boolean values, but as string, and they are not converted back to boolean values. To filter on boolean values, you have to check for the strings 'true' and '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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top