Andrea
Andrea

Reputation: 20493

Getting raw results from Django Haystack

I have site using Django Haystack (with the Whoosh backend). I would like to be able to get all results for a given model matching a given query. Something like

from haystack import get_results

result_list = get_results(model=MyModel, query='foo')

In the documentation I have found a lot of stuff about customizing the default views and forms, advanced searching and so on, but I cannot find anything for the simple task of getting all the models matching a query and managing them on my own. Is this possible?

Upvotes: 1

Views: 948

Answers (2)

Ashok Joshi
Ashok Joshi

Reputation: 448

You can use SearchQuerySet like : -

SearchQuerySet().filter(content='abra').models(MyModel)

It will only return result from MyModel model.

Upvotes: 0

jpic
jpic

Reputation: 33420

You can use SearchQuerySet. For example:

In [1]: from haystack.query import SearchQuerySet

In [2]: SearchQuerySet().filter(content='abra')
Out[2]: [<SearchResult: art.artist (pk=u'23')>, <SearchResult: art.artwork (pk=u'191')>]

In [3]: SearchQuerySet().filter(content='abra').count()
Out[3]: 2

Upvotes: 2

Related Questions