diff --git a/docs/changelog.rst b/docs/changelog.rst index 809650f3..e3f3ecd1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in 0.9.X - DEV ====================== +- OperationError: Shard Keys are immutable. Tried to update id even though the document is not yet saved #771 - with_limit_and_skip for count should default like in pymongo #759 - Fix storing value of precision attribute in DecimalField #787 - Querying by a field defined in a subclass raises InvalidQueryError #744 diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index bf5bdf79..8995a304 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -386,9 +386,9 @@ class BaseDocument(object): return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs) @classmethod - def from_json(cls, json_data): + def from_json(cls, json_data, created=False): """Converts json data to an unsaved document instance""" - return cls._from_son(json_util.loads(json_data)) + return cls._from_son(json_util.loads(json_data), created=created) def __expand_dynamic_values(self, name, value): """expand any dynamic values to their correct types / values""" @@ -617,7 +617,7 @@ class BaseDocument(object): return cls._meta.get('collection', None) @classmethod - def _from_son(cls, son, _auto_dereference=True, only_fields=[]): + def _from_son(cls, son, _auto_dereference=True, only_fields=[], created=False): """Create an instance of a Document (subclass) from a PyMongo SON. """ @@ -667,7 +667,7 @@ class BaseDocument(object): if cls.STRICT: data = dict((k, v) for k, v in data.iteritems() if k in cls._fields) - obj = cls(__auto_convert=False, _created=False, __only_fields=only_fields, **data) + obj = cls(__auto_convert=False, _created=created, __only_fields=only_fields, **data) obj._changed_fields = changed_fields if not _auto_dereference: obj._fields = fields diff --git a/tests/document/instance.py b/tests/document/instance.py index a22f3fbf..b7733548 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -2719,5 +2719,16 @@ class InstanceTest(unittest.TestCase): self.assertEquals(p4.height, 189) self.assertEquals(Person.objects(height=189).count(), 1) + def test_from_son(self): + # 771 + class MyPerson(self.Person): + meta = dict(shard_key=["id"]) + p = MyPerson.from_json('{"name": "name", "age": 27}', created=True) + self.assertEquals(p.id, None) + p.id = "12345" # in case it is not working: "OperationError: Shard Keys are immutable..." will be raised here + p = MyPerson._from_son({"name": "name", "age": 27}, created=True) + self.assertEquals(p.id, None) + p.id = "12345" # in case it is not working: "OperationError: Shard Keys are immutable..." will be raised here + if __name__ == '__main__': unittest.main()