for p in db.collection.find({\"test_set\":\"abc\"}):
posts.append(p)
thejson = json.dumps({\'results\':posts})
return HttpResponse(thejson, mimetype=\"application/j
It's pretty easy to write a custom serializer which copes with the ObjectIds. Django already includes one which handles decimals and dates, so you can extend that:
from django.core.serializers.json import DjangoJSONEncoder
from bson import objectid
class MongoAwareEncoder(DjangoJSONEncoder):
"""JSON encoder class that adds support for Mongo objectids."""
def default(self, o):
if isinstance(o, objectid.ObjectId):
return str(o)
else:
return super(MongoAwareEncoder, self).default(o)
Now you can just tell json
to use your custom serializer:
thejson = json.dumps({'results':posts}, cls=MongoAwareEncoder)