[Answered ]-Convert mongoengine queryset to a json file

2👍

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 ]
        else:
            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