diff --git a/AUTHORS b/AUTHORS index 0d64a565..163ee8fe 100644 --- a/AUTHORS +++ b/AUTHORS @@ -186,3 +186,4 @@ that much better: * rfkrocktk (https://github.com/rfkrocktk) * Gustavo Andrés Angulo (https://github.com/woakas) * Dmytro Popovych (https://github.com/drudim) + * Tom (https://github.com/tomprimozic) \ No newline at end of file diff --git a/docs/changelog.rst b/docs/changelog.rst index 76157220..fa01df85 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,7 @@ Changelog Changes in 0.8.5 ================ +- Fixed EmbeddedDocument with ReferenceField equality issue (#502) - Fixed GenericReferenceField serialization order (#499) - Fixed count and none bug (#498) - Fixed bug with .only() and DictField with digit keys (#496) diff --git a/mongoengine/document.py b/mongoengine/document.py index 1bbd7b73..59bea359 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -66,7 +66,7 @@ class EmbeddedDocument(BaseDocument): def __eq__(self, other): if isinstance(other, self.__class__): - return self._data == other._data + return self.to_mongo() == other.to_mongo() return False def __ne__(self, other): diff --git a/tests/document/instance.py b/tests/document/instance.py index a61c4396..06b884f5 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -2392,6 +2392,24 @@ class InstanceTest(unittest.TestCase): system = System.objects.first() self.assertEqual("UNDEFINED", system.nodes["node"].parameters["param"].macros["test"].value) + def test_embedded_document_equality(self): + + class Test(Document): + field = StringField(required=True) + + class Embedded(EmbeddedDocument): + ref = ReferenceField(Test) + + Test.drop_collection() + test = Test(field='123').save() # has id + + e = Embedded(ref=test) + f1 = Embedded._from_son(e.to_mongo()) + f2 = Embedded._from_son(e.to_mongo()) + + self.assertEqual(f1, f2) + f1.ref # Dereferences lazily + self.assertEqual(f1, f2) if __name__ == '__main__': unittest.main()