python - How to filter Haystack SearchQuerySets by related models -
how filter/join haystack searchqueryset related model fields?
i have query like:
sqs = searchqueryset().models(models.person) and returns same results equivalent admin page returns.
however, if try , filter model records linked foreign key:
sqs = sqs.filter(workplace__role__name='teacher') it returns nothing, though page /admin/myapp/person/?workplace__role__name=teacher returns several records.
i don't want full-text searching of these related models. want simple exact-match filter. possible haystack?
you cannot perform joins using search engine ones supported haystack. make queries need add information want filter on in "denormalized" fashion in search index:
class profileindex(indexes.searchindex, indexes.indexable): # other fields, model attributes role_name = indexes.charfield() def get_model(self): return person def prepare_role_name(self, person): return person.workplace.role_name then can filter on field role_name. make sure update index if eg. name changes, have update according entries in search index.
Comments
Post a Comment