Django Haystack - Unindexed Search for Many Fields

Is there a way to have only the filtered part of the model as a SeachQuerySet?

Sort of:

query = SearchQuerySet (). models (Entry.filter (categories__name = 'something'))

instead

query = SearchQuerySet (). models (Entry)

The field I want to filter is a multi-tone field and is not indexed.

+6
source share
1 answer

The search index does not store any relationships, so it is "flat." You can only add category identifiers to the index for Entry (note that you must use the prepare_ method for prepare_ ):

 class EntryIndex(indexes.SearchIndex, indexes.Indexable): # your other fields categories = MultiValueField() def prepare_categories(self, obj): return [category.pk for category in obj.categories.all()] 

You can do something like:

 category = Category.objects.get(name='something') sqs = SearchQuerySet().models(Entry).filter(categories=category.pk) 
+14
source

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


All Articles