Trying to serialize a MongoDB cursor in Django
import json
from pymongo import json_util
results = json.dumps(results, default=json_util.default, separators=(',', ':'))
Where the original results is something like
[{u'_id': ObjectId('4f7c0f34705ff8294a00006f'),
u'identifier': u'1',
u'items': [{u'amount': 9.99, u'name': u'PapayaWhip', u'quantity': 1}],
u'location': None,
u'timestamp': datetime.datetime(2012, 4, 4, 10, 7, 0, 596000),
u'total': 141.25}]
Edit: Obtained by using something like
from django.db import connections
connection = connections['default']
results = connection.get_collection('papayas_papaya')
results = results.find({
'identifier': '1',
})
Gives me
TypeError: <django_mongodb_engine.utils.DebugCursor object> is not JSON serializable
Does anyone know what I'm doing wrong?
Using json_util should serialize MongoDB documents, maybe my issue is that I'm trying to serliaze a cursor. (How do I get the document from the cursor? A simple tuple "cast"?)
Cheers!
result.resultslooks like; to obtain it I used regular PyMongo query. I've updated the question anyway :-)