Haystack whoosh () models do not narrow

I have the following query

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

but it also returns other models, I would like to see only instances of the location.

Using Haystack 2.1.0 and whoosh 2.5

Any ideas?

+4
source share
3 answers

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

+3
source

I ran into the same problem that Model filtering is ignored. I was able to get .models () while working by downgrading to Haystack 2.0.0 and Whoosh 2.4.1

+1
source

This partly depends on the answer of James Lems , but this should work for any version of Haystack and Whoosh. Unfortunately, none of the parties actually comes to the rescue, but the solution below seems not 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 when SearchQuerySet when SearchQuerySet use MySearchQuerySet instead:

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

Source: https://habr.com/ru/post/1493988/


All Articles