Search code examples
djangodjango-haystack

How to return only indexed objects of a specific type in Haystack


Is there any way to use SearchQuerySet and restrict the results to only a specific indexed model? i.e. If i add Note and NoteIndex to Haystack, can I pull out just results that correspond to Note instances?

EDIT:

I have had a look and found that there is a reserved field named django_ct that is stored on every indexed model. Is it possible to filter on this field? What values does it take?

DOUBLE EDIT:

Nevermind. After reading the Haystack source code, django_ct is 'appname.modelname' internally and can be querired with SearchQuerySet.filter(django_ct = 'appname.modelname')


Solution

  • According to the Haystack documentation, a SearchQueryset object has a method called models() that restricts the results to those models.

    e.g.

    SearchQuerySet().models(BlogEntry, Comment).filter(content='foo')
    

    As you see, it uses the actual model class. My guess is that it uses this to lookup the content-type to perform filter.