Merge pull request #796 from aericson/fix_dynamic_document_reload
Fix KeyError on reload() from a DynamicDocument
This commit is contained in:
		
							
								
								
									
										1
									
								
								AUTHORS
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								AUTHORS
									
									
									
									
									
								
							| @@ -212,3 +212,4 @@ that much better: | |||||||
|  * Axel Haustant (https://github.com/noirbizarre) |  * Axel Haustant (https://github.com/noirbizarre) | ||||||
|  * David Czarnecki (https://github.com/czarneckid) |  * David Czarnecki (https://github.com/czarneckid) | ||||||
|  * Vyacheslav Murashkin (https://github.com/a4tunado) |  * Vyacheslav Murashkin (https://github.com/a4tunado) | ||||||
|  |  * André Ericson (https://github.com/aericson) | ||||||
|   | |||||||
| @@ -543,7 +543,13 @@ class Document(BaseDocument): | |||||||
|  |  | ||||||
|         for field in self._fields_ordered: |         for field in self._fields_ordered: | ||||||
|             if not fields or field in fields: |             if not fields or field in fields: | ||||||
|  |                 try: | ||||||
|                     setattr(self, field, self._reload(field, obj[field])) |                     setattr(self, field, self._reload(field, obj[field])) | ||||||
|  |                 except KeyError: | ||||||
|  |                     # If field is removed from the database while the object | ||||||
|  |                     # is in memory, a reload would cause a KeyError | ||||||
|  |                     # i.e. obj.update(unset__field=1) followed by obj.reload() | ||||||
|  |                     delattr(self, field) | ||||||
|  |  | ||||||
|         self._changed_fields = obj._changed_fields |         self._changed_fields = obj._changed_fields | ||||||
|         self._created = False |         self._created = False | ||||||
|   | |||||||
| @@ -81,6 +81,13 @@ class DynamicTest(unittest.TestCase): | |||||||
|         obj = collection.find_one() |         obj = collection.find_one() | ||||||
|         self.assertEqual(sorted(obj.keys()), ['_cls', '_id', 'name']) |         self.assertEqual(sorted(obj.keys()), ['_cls', '_id', 'name']) | ||||||
|  |  | ||||||
|  |     def test_reload_after_unsetting(self): | ||||||
|  |         p = self.Person() | ||||||
|  |         p.misc = 22 | ||||||
|  |         p.save() | ||||||
|  |         p.update(unset__misc=1) | ||||||
|  |         p.reload() | ||||||
|  |  | ||||||
|     def test_dynamic_document_queries(self): |     def test_dynamic_document_queries(self): | ||||||
|         """Ensure we can query dynamic fields""" |         """Ensure we can query dynamic fields""" | ||||||
|         p = self.Person() |         p = self.Person() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user