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:
Ross Lawley
2012-11-08 12:04:14 +00:00
parent 4b45c0cd14
commit b8d53a6f0d
8 changed files with 201 additions and 20 deletions

View File

@@ -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: