Merge pull request #798 from DavidBord/fix-771
fix-#771: OperationError: Shard Keys are immutable. Tried to update id e...
This commit is contained in:
		| @@ -5,6 +5,7 @@ Changelog | |||||||
|  |  | ||||||
| Changes in 0.9.X - DEV | 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 | - with_limit_and_skip for count should default like in pymongo #759 | ||||||
| - Fix storing value of precision attribute in DecimalField #787 | - Fix storing value of precision attribute in DecimalField #787 | ||||||
| - Querying by a field defined in a subclass raises InvalidQueryError #744 | - Querying by a field defined in a subclass raises InvalidQueryError #744 | ||||||
|   | |||||||
| @@ -386,9 +386,9 @@ class BaseDocument(object): | |||||||
|         return json_util.dumps(self.to_mongo(use_db_field),  *args, **kwargs) |         return json_util.dumps(self.to_mongo(use_db_field),  *args, **kwargs) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def from_json(cls, json_data): |     def from_json(cls, json_data, created=False): | ||||||
|         """Converts json data to an unsaved document instance""" |         """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): |     def __expand_dynamic_values(self, name, value): | ||||||
|         """expand any dynamic values to their correct types / values""" |         """expand any dynamic values to their correct types / values""" | ||||||
| @@ -617,7 +617,7 @@ class BaseDocument(object): | |||||||
|         return cls._meta.get('collection', None) |         return cls._meta.get('collection', None) | ||||||
|  |  | ||||||
|     @classmethod |     @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. |         """Create an instance of a Document (subclass) from a PyMongo SON. | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
| @@ -667,7 +667,7 @@ class BaseDocument(object): | |||||||
|         if cls.STRICT: |         if cls.STRICT: | ||||||
|             data = dict((k, v) |             data = dict((k, v) | ||||||
|                         for k, v in data.iteritems() if k in cls._fields) |                         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 |         obj._changed_fields = changed_fields | ||||||
|         if not _auto_dereference: |         if not _auto_dereference: | ||||||
|             obj._fields = fields |             obj._fields = fields | ||||||
|   | |||||||
| @@ -2719,5 +2719,16 @@ class InstanceTest(unittest.TestCase): | |||||||
|         self.assertEquals(p4.height, 189) |         self.assertEquals(p4.height, 189) | ||||||
|         self.assertEquals(Person.objects(height=189).count(), 1) |         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__': | if __name__ == '__main__': | ||||||
|     unittest.main() |     unittest.main() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user