Haystack whoosh models() not narrowing models

独自空忆成欢 提交于 2019-12-07 11:35:01

问题


I have the following query

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

but it's returning other models as well, I would only want to see Location instances.

Using Haystack 2.1.0 and whoosh 2.5

Any ideas?


回答1:


My current work around is to use filter(django_ct='app_name.model')




回答2:


I ran into the same issue with Model filtering being ignored. I was able to get .models() working by downgrading to Haystack 2.0.0 and Whoosh 2.4.1




回答3:


This is based partly on James Lims answer, but this should work for any versions of Haystack and Whoosh. Unfortunately neither party is really coming to the rescue on this, but the below solution doesn't seem to be too bad.

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)

Then where ever SearchQuerySet use MySearchQuerySet instead:

MySearchQuery().filter(name="foo").models(my_models.bar,my_models.baz)


来源:https://stackoverflow.com/questions/17914986/haystack-whoosh-models-not-narrowing-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!