Python 大海捞针搜索非主字段

Python 大海捞针搜索非主字段,python,django,django-haystack,Python,Django,Django Haystack,我想从两个不同的字段中搜索索引模型。例如,有时按姓名搜索,有时按职业搜索。有人知道采取正确的方法吗?这是我当前的search_index.py文件: class JobIndex(indexes.SearchIndex): text = indexes.CharField(document=True) name = indexes.CharField(model_attr='name') occupation = indexes.CharField(model_attr=

我想从两个不同的字段中搜索索引模型。例如,有时按姓名搜索,有时按职业搜索。有人知道采取正确的方法吗?这是我当前的search_index.py文件:

class JobIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True)
    name = indexes.CharField(model_attr='name')
    occupation = indexes.CharField(model_attr='occupation')

    def prepare(self, obj):
        self.prepared_data = super(JobIndex, self).prepare(obj)
        self.prepared_data['text'] = obj.name
        return self.prepared_data
    def get_queryset(self):
        return Job.objects.filter(status='open')
site.register(Job, JobIndex)

正确的方法是使用带有过滤器的SearchQuerySet:

在您的情况下,它看起来像:

from haystack.query import SearchQuerySet

sqs = SearchQuerySet()

# Find people named Bob
sqs.filter(name="Bob")
# Find people who are developers
sqs.filter(occupation="developer")
# Or chain searches: Find developers named Bob
sqs.filter(occupation="developer").filter(name="Bob")