搜索索引中的 Django haystack 过滤器

标签 django python-2.7 django-haystack whoosh

我的项目中的搜索工作正常。但在我的模型中,我有一个名为 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/

相关文章:

django - 在 Docker 中安装地理空间库

python-2.7 - 重命名 matplotlib 中的 x 轴值

python - 从 Django Haystack 中的外键搜索

python - 安装 "elasticsearch"而不是 "pyelasticsearch"

css - 如何在生产中找到django静态文件

django - 内存数据库django中的Postgresql

python-2.7 - Python 中的动画条形图

python - 将日期时间插入 MySql 数据库

python - MapperParsingException 在使用 elasticsearch 在 django-haystack 中执行 rebuild_index

python - 在 Django Rest Framework 中发布外键 ID