Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Haystack Query Serialization

I'm trying to serialize a HayStack SearchQuerySet:

from django.core import serializers
serializers.serialize("json", SearchQuerySet().filter(content=request.GET['q']))

but it throws:

'SearchQuery' object has no attribute '_build_query'

How can I fix this?

like image 503
Alon Gubkin Avatar asked Dec 08 '10 13:12

Alon Gubkin


2 Answers

I don't recommend call 'object' per result as it would hit to database and beat purpose of search performance. Instead consider calling get_stored_fields method which can be used with json dumps:

import simplejson as json
data = map(lambda x: x.get_stored_fields(), search_result)
json.dumps(data)
like image 145
hurturk Avatar answered Sep 28 '22 19:09

hurturk


I had faced a similar problem. used something like this and it worked:

serializers.serialize("json", [x.object for x in queryset]

like image 35
Krishna Bharadwaj Avatar answered Sep 28 '22 20:09

Krishna Bharadwaj