Merge pull request #1675 from erdenezul/push_list_of_list

use each modifier only with $position  #1673
This commit is contained in:
Emmanuel Leblond 2017-11-15 08:43:29 +01:00 committed by GitHub
commit 4d6256e1a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -344,8 +344,6 @@ def update(_doc_cls=None, **update):
if not isinstance(value, (set, tuple, list)): if not isinstance(value, (set, tuple, list)):
value = [value] value = [value]
value = {key: {'$each': value, '$position': position}} value = {key: {'$each': value, '$position': position}}
elif isinstance(value, list):
value = {key: {'$each': value}}
else: else:
value = {key: value} value = {key: value}
else: else:

View File

@ -3183,6 +3183,17 @@ class InstanceTest(unittest.TestCase):
blog.reload() blog.reload()
self.assertEqual(blog.tags, ['mongodb', 'code', 'python']) self.assertEqual(blog.tags, ['mongodb', 'code', 'python'])
def test_push_nested_list(self):
"""Ensure that push update works in nested list"""
class BlogPost(Document):
slug = StringField()
tags = ListField()
blog = BlogPost(slug="test").save()
blog.update(push__tags=["value1", 123])
blog.reload()
self.assertEqual(blog.tags, [["value1", 123]])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -1929,6 +1929,21 @@ class QuerySetTest(unittest.TestCase):
post.reload() post.reload()
self.assertEqual(post.tags, ['scala', 'mongodb', 'python', 'java']) self.assertEqual(post.tags, ['scala', 'mongodb', 'python', 'java'])
def test_update_push_list_of_list(self):
"""Ensure that the 'push' update operation works in the list of list
"""
class BlogPost(Document):
slug = StringField()
tags = ListField()
BlogPost.drop_collection()
post = BlogPost(slug="test").save()
BlogPost.objects.filter(slug="test").update(push__tags=["value1", 123])
post.reload()
self.assertEqual(post.tags, [["value1", 123]])
def test_update_push_and_pull_add_to_set(self): def test_update_push_and_pull_add_to_set(self):
"""Ensure that the 'pull' update operation works correctly. """Ensure that the 'pull' update operation works correctly.
""" """