Merge remote branch 'upstream/master' into dev
This commit is contained in:
commit
c379ff883a
@ -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
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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):
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user