I use haystack to fully search the site in my project, which is looking for models of books, authors, events and videos.
Then I have the main page of the book where I want to search only against the Books model.
I found this post: How to return only indexed objects of a specific type in Haystack
However, it does not seem to work for me. I am testing locally using a simple backend, and I know there are some problems, and I'm not sure if this is related.
my search_indexes.py file looks like this:
class BookSearchIndex (SearchIndex): text = CharField(document=True, use_template=True) title_web = CharField(model_attr='title_web', boost=1.125) on_sale_date = CharField(model_attr='on_sale_date') def index_queryset(self): return Book.objects.active().filter(publish_level='published') site.register(Book, BookSearchIndex)
And, in my opinion, if a search query was passed, return only books with this query, otherwise show all the books:
search = self.request.GET.get('search', None) if search: clean_query = SearchQuerySet().query.clean(search) sqs = SearchQuerySet().models(Book).filter(content=clean_query).order_by('-on_sale_date') else: sqs = SearchQuerySet().models(Book).order_by('-on_sale_date)
The search correctly filters the element based on the search query, but it still returns all the models. This does not limit it only to the model of the book.
This part seems to be inactive:
SearchQuerySet().models(Book)
Can someone help me figure out what I'm doing wrong?