Added json serialisation support
- Added to_json and from_json to Document (MongoEngine/mongoengine#1) - Added to_json and from_json to QuerySet (MongoEngine/mongoengine#131)
This commit is contained in:
		| @@ -6,6 +6,7 @@ import re | ||||
| import warnings | ||||
|  | ||||
| from bson.code import Code | ||||
| from bson import json_util | ||||
| import pymongo | ||||
| from pymongo.common import validate_read_preference | ||||
|  | ||||
| @@ -1216,6 +1217,15 @@ class QuerySet(object): | ||||
|         max_depth += 1 | ||||
|         return self._dereference(self, max_depth=max_depth) | ||||
|  | ||||
|     def to_json(self): | ||||
|         """Converts a queryset to JSON""" | ||||
|         return json_util.dumps(self._collection_obj.find(self._query)) | ||||
|  | ||||
|     def from_json(self, json_data): | ||||
|         """Converts json data to unsaved objects""" | ||||
|         son_data = json_util.loads(json_data) | ||||
|         return [self._document._from_son(data) for data in son_data] | ||||
|  | ||||
|     @property | ||||
|     def _dereference(self): | ||||
|         if not self.__dereference: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user