How to index ManyToMany field in django?

Are you facing problems to include ManyToMany field in django haystack indexing? Then here is the solution.

Indexing a single valued field is not that much hard. When it comes for multi-valued field, a big question comes into your mind. In this case, index_obj=indexes.MultiValueField(indexed=True, stored=True) will help you.

Since we cannot assign multi-valued fields directly, include following code in


             return [index_obj.attribute for index_obj in obj.index_obj.select_related()]

Now, you have indexed your ManyToMany field. Then go to your and find the ManyToMany object which belongs to search keyword.

index_obj = MultiValuedModel.objects.get(name=request.GET.get(‘SearchKeyword’)

Thats it…, Now you can filter SearchQuerySet() by index_obj parameter. This is how you can index ManyToMany field.

Refer this link to get into more depth


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>