diff --git a/docs/changelog.rst b/docs/changelog.rst index df24a5f6..b61a06d6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in 0.8.2 ================ +- Fixed hashing of EmbeddedDocuments (#348) - Added lock when calling doc.Delete() for when signals have no sender (#350) - Reload forces read preference to be PRIMARY (#355) - Querysets are now lest restrictive when querying duplicate fields (#332, #333) diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index e2944fb0..ca154a29 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -215,7 +215,7 @@ class BaseDocument(object): return not self.__eq__(other) def __hash__(self): - if self.pk is None: + if getattr(self, 'pk', None) is None: # For new object return super(BaseDocument, self).__hash__() else: diff --git a/tests/document/instance.py b/tests/document/instance.py index cdc6fe08..f29cec2e 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -1705,6 +1705,14 @@ class InstanceTest(unittest.TestCase): self.assertTrue(u1 in all_user_set) + def test_embedded_document_hash(self): + """Test embedded document can be hashed + """ + class User(EmbeddedDocument): + pass + + hash(User()) + def test_picklable(self): pickle_doc = PickleTest(number=1, string="One", lists=['1', '2'])