diff --git a/docs/guide/defining-documents.rst b/docs/guide/defining-documents.rst index de0e7272..e333674e 100644 --- a/docs/guide/defining-documents.rst +++ b/docs/guide/defining-documents.rst @@ -134,8 +134,8 @@ document class as the first argument:: class Page(Document): comments = ListField(EmbeddedDocumentField(Comment)) - comment1 = Comment('Good work!') - comment2 = Comment('Nice article!') + comment1 = Comment(content='Good work!') + comment2 = Comment(content='Nice article!') page = Page(comments=[comment1, comment2]) Dictionary Fields diff --git a/mongoengine/base.py b/mongoengine/base.py index ae9e1eb3..b7f7ce87 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -508,6 +508,17 @@ class BaseDocument(object): return True return False + def __ne__(self, other): + return not self.__eq__(other) + + def __hash__(self): + """ For list, dic key """ + if self.pk is None: + # For new object + return super(BaseDocument,self).__hash__() + else: + return hash(self.pk) + if sys.version_info < (2, 5): # Prior to Python 2.5, Exception was an old-style class import types diff --git a/mongoengine/document.py b/mongoengine/document.py index e64092e8..196662c3 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -67,7 +67,7 @@ class Document(BaseDocument): :param safe: check if the operation succeeded before returning :param force_insert: only try to create a new document, don't allow updates of existing documents - :param validate: validates the document; set to ``False`` for skiping + :param validate: validates the document; set to ``False`` to skip. """ if validate: self.validate() diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 3d78e9e3..c06fdd4d 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -663,9 +663,6 @@ class GridFSProxy(object): def close(self): if self.newfile: self.newfile.close() - else: - msg = "The close() method is only necessary after calling write()" - warnings.warn(msg) class FileField(BaseField): diff --git a/tests/document.py b/tests/document.py index 67c21a46..0da7b93e 100644 --- a/tests/document.py +++ b/tests/document.py @@ -729,6 +729,54 @@ class DocumentTest(unittest.TestCase): def tearDown(self): self.Person.drop_collection() + def test_document_hash(self): + """Test document in list, dict, set + """ + class User(Document): + pass + + class BlogPost(Document): + pass + + # Clear old datas + User.drop_collection() + BlogPost.drop_collection() + + u1 = User.objects.create() + u2 = User.objects.create() + u3 = User.objects.create() + u4 = User() # New object + + b1 = BlogPost.objects.create() + b2 = BlogPost.objects.create() + + # in List + all_user_list = list(User.objects.all()) + + self.assertTrue(u1 in all_user_list) + self.assertTrue(u2 in all_user_list) + self.assertTrue(u3 in all_user_list) + self.assertFalse(u4 in all_user_list) # New object + self.assertFalse(b1 in all_user_list) # Other object + self.assertFalse(b2 in all_user_list) # Other object + + # in Dict + all_user_dic = {} + for u in User.objects.all(): + all_user_dic[u] = "OK" + + self.assertEqual(all_user_dic.get(u1, False), "OK" ) + self.assertEqual(all_user_dic.get(u2, False), "OK" ) + self.assertEqual(all_user_dic.get(u3, False), "OK" ) + self.assertEqual(all_user_dic.get(u4, False), False ) # New object + self.assertEqual(all_user_dic.get(b1, False), False ) # Other object + self.assertEqual(all_user_dic.get(b2, False), False ) # Other object + + # in Set + all_user_set = set(User.objects.all()) + + self.assertTrue(u1 in all_user_set ) + if __name__ == '__main__': unittest.main()