for p in db.collection.find({"test_set":"abc"}):
posts.append(p)
thejson = json.dumps({'results':posts})
return HttpResponse(thejson, mimetype="application/javascript")
在我的 Django/Python 代码中,由于“ObjectID”,我无法从 mongo 查询返回 JSON。错误提示“ObjectID”不可序列化。
我该怎么办? 一个hacky的方法是循环:
for p in posts:
p['_id'] = ""
最佳答案
json 模块由于 ObjectID 等原因无法工作。
幸运的是 PyMongo提供json_util其中...
... allow[s] for specialized encoding and decoding of BSON documents into Mongo Extended JSON's Strict mode. This lets you encode / decode BSON documents to JSON even when they use special BSON types.
https://stackoverflow.com/questions/4404742/