2013-04-05 60 views
2

我的項目中有一項搜索工作正常。但在我的模型中,我有一個名爲is_active的布爾字段。搜索索引中的Django haystack過濾器

我希望只有當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。有什麼想法嗎?

+0

你使用的是乾草堆版本? – Ponytech 2013-04-08 16:21:47

+0

Haystack 1.2.7和whoosh 2.4.1 – 2013-04-08 18:19:08

回答

2

在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)