干草堆嗖嗖的模特()没有缩小模型

时间:2013-07-29 02:00:13

标签: models django-haystack whoosh

我有以下查询

locations = SearchQuerySet().filter_or(content__in=words).models(Location)

但它也会返回其他模型,我只想查看位置实例。

使用Haystack 2.1.0和whoosh 2.5

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

我目前的工作是使用filter(django_ct='app_name.model')

答案 1 :(得分:1)

我遇到了同样的问题,忽略了模型过滤。我能够通过降级到Haystack 2.0.0和Whoosh 2.4.1来获得.models()的工作

答案 2 :(得分:0)

This is based partly on James Lims answer,但这适用于任何版本的Haystack和Whoosh。不幸的是,任何一方都没有真正解决这个问题,但下面的解决方案似乎并不太糟糕。

class MySearchQuerySet(SearchQuerySet):
    def models(self,*mods):
        # We have to redefine this because Whoosh & Haystack don't play well with model filtering
        from haystack.utils import get_model_ct
        mods = [get_model_ct(m) for m in mods]
        return self.filter(django_ct__in=mods)

然后SearchQuerySet改为使用MySearchQuerySet

MySearchQuery().filter(name="foo").models(my_models.bar,my_models.baz)
相关问题