我的项目中的搜索工作正常。但在我的模型中,我有一个名为 is_active
的 bool 字段。
我希望仅当 is_active 为 True 时才进行搜索,但我一直在测试这一点,但没有任何令人满意的响应。
我的search_indexes.py:
from haystack.indexes import *
from haystack.sites import site
from core.models import AnuncioSolucao
class AnuncioSolucaoIndex(RealTimeSearchIndex):
text = CharField(document=True,use_template=True)
site.register(AnuncioSolucao,AnuncioSolucaoIndex)
这样就可以了,但也给我带来所有的is_active == False
。有什么想法吗?
最佳答案
SearchIndex API 上有一个名为 read_queryset 的方法。我只需要重写这个:
class AnuncioSolucaoIndex(RealTimeSearchIndex):
text = CharField(document=True,use_template=True)
def read_queryset(self):
super(AnuncioSolucaoIndex,self).read_queryset()
return AnuncioSolucao.objects.filter(is_active=True)
关于搜索索引中的 Django haystack 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15827095/