EmbeddedDocument._instance is now set when settng the attribute (#506)
This commit is contained in:
		| @@ -4,6 +4,7 @@ Changelog | |||||||
|  |  | ||||||
| Changes in 0.8.5 | Changes in 0.8.5 | ||||||
| ================ | ================ | ||||||
|  | - EmbeddedDocument._instance is now set when settng the attribute (#506) | ||||||
| - Fixed EmbeddedDocument with ReferenceField equality issue (#502) | - Fixed EmbeddedDocument with ReferenceField equality issue (#502) | ||||||
| - Fixed GenericReferenceField serialization order (#499) | - Fixed GenericReferenceField serialization order (#499) | ||||||
| - Fixed count and none bug (#498) | - Fixed count and none bug (#498) | ||||||
|   | |||||||
| @@ -89,12 +89,7 @@ class BaseField(object): | |||||||
|             return self |             return self | ||||||
|  |  | ||||||
|         # Get value from document instance if available |         # Get value from document instance if available | ||||||
|         value = instance._data.get(self.name) |         return instance._data.get(self.name) | ||||||
|  |  | ||||||
|         EmbeddedDocument = _import_class('EmbeddedDocument') |  | ||||||
|         if isinstance(value, EmbeddedDocument) and value._instance is None: |  | ||||||
|             value._instance = weakref.proxy(instance) |  | ||||||
|         return value |  | ||||||
|  |  | ||||||
|     def __set__(self, instance, value): |     def __set__(self, instance, value): | ||||||
|         """Descriptor for assigning a value to a field in a document. |         """Descriptor for assigning a value to a field in a document. | ||||||
| @@ -116,6 +111,10 @@ class BaseField(object): | |||||||
|                 # Values cant be compared eg: naive and tz datetimes |                 # Values cant be compared eg: naive and tz datetimes | ||||||
|                 # So mark it as changed |                 # So mark it as changed | ||||||
|                 instance._mark_as_changed(self.name) |                 instance._mark_as_changed(self.name) | ||||||
|  |  | ||||||
|  |         EmbeddedDocument = _import_class('EmbeddedDocument') | ||||||
|  |         if isinstance(value, EmbeddedDocument) and value._instance is None: | ||||||
|  |             value._instance = weakref.proxy(instance) | ||||||
|         instance._data[self.name] = value |         instance._data[self.name] = value | ||||||
|  |  | ||||||
|     def error(self, message="", errors=None, field_name=None): |     def error(self, message="", errors=None, field_name=None): | ||||||
|   | |||||||
| @@ -490,6 +490,26 @@ class InstanceTest(unittest.TestCase): | |||||||
|         doc = Doc.objects.get() |         doc = Doc.objects.get() | ||||||
|         self.assertEqual(doc, doc.embedded_field[0]._instance) |         self.assertEqual(doc, doc.embedded_field[0]._instance) | ||||||
|  |  | ||||||
|  |     def test_instance_is_set_on_setattr(self): | ||||||
|  |  | ||||||
|  |         class Email(EmbeddedDocument): | ||||||
|  |             email = EmailField() | ||||||
|  |             def clean(self): | ||||||
|  |                 print "instance:" | ||||||
|  |                 print self._instance | ||||||
|  |  | ||||||
|  |         class Account(Document): | ||||||
|  |             email = EmbeddedDocumentField(Email) | ||||||
|  |  | ||||||
|  |         Account.drop_collection() | ||||||
|  |         acc = Account() | ||||||
|  |         acc.email = Email(email='test@example.com') | ||||||
|  |         self.assertTrue(hasattr(acc._data["email"], "_instance")) | ||||||
|  |         acc.save() | ||||||
|  |  | ||||||
|  |         acc1 = Account.objects.first() | ||||||
|  |         self.assertTrue(hasattr(acc1._data["email"], "_instance")) | ||||||
|  |  | ||||||
|     def test_document_clean(self): |     def test_document_clean(self): | ||||||
|         class TestDocument(Document): |         class TestDocument(Document): | ||||||
|             status = StringField() |             status = StringField() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user