Python を使用して、Mongo ドキュメントのクエリセットのデータを .json ファイルにダンプするにはどうすればよいですか?
私はdjangoシリアライザーを使用しようとしましたが、フィールドがmongoでアクセスされるのと同じ方法でdjangoでアクセスされないため、機能しませんでした.
for model in models:
json_serializer.serialize(model.objects.all(), indent=2, stream=output_file_mongo)
また、python JSON エンコード/デコーダーを使用しようとしましたが、
import json
for model in mongo_models:
output_file_mongo.write(json.dumps(model.objects.all()))
例外が発生します
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: [<MongoModelA: MongoModelA object>] is not JSON serializable