[Answered ]-Convert mongoengine queryset to a json file


The django serializer does not know how to handle mongoengine objects, You’ll most likely have to write your own json encoder to map them to a simple dictionary:

class MyEncoder(json.JSONEncoder):
    def encode_object(self, obj):
        return { 'id':unicode(obj.id), 'other_property': obj.other_property }

    def default(self, obj):
        if hasattr(obj, '__iter__'):
            return [ self.encode_object(x) for x in obj ]
            return self.encode_object(obj)

then call it like so:

import json
import MyEncoder

json_string = json.dumps(model.objects.all(), cls=MyEncoder)

Leave a comment