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:
@@ -2,6 +2,7 @@ import operator
|
||||
from functools import partial
|
||||
|
||||
import pymongo
|
||||
from bson import json_util
|
||||
from bson.dbref import DBRef
|
||||
|
||||
from mongoengine import signals
|
||||
@@ -253,6 +254,15 @@ class BaseDocument(object):
|
||||
if errors:
|
||||
raise ValidationError('ValidationError', errors=errors)
|
||||
|
||||
def to_json(self):
|
||||
"""Converts a document to JSON"""
|
||||
return json_util.dumps(self.to_mongo())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_data):
|
||||
"""Converts json data to an unsaved document instance"""
|
||||
return cls._from_son(json_util.loads(json_data))
|
||||
|
||||
def __expand_dynamic_values(self, name, value):
|
||||
"""expand any dynamic values to their correct types / values"""
|
||||
if not isinstance(value, (dict, list, tuple)):
|
||||
|
||||
Reference in New Issue
Block a user