Вопрос

I have a search working fine on my project. But in my models I have a boolean field named is_active.

I want the search occurs only when is_active is True, but I've been testing this without any satisfatory response.

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

That way it works, but also bring me all the is_active == False. Any thoughts?

Это было полезно?

Решение

There's a method called read_queryset on the SearchIndex API. I just had to override this:

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top