Fix bug #1965 of $position and $push operators do not work with list in an EmbeddedDocument. Set key value to joined parts excluding the index at the end. Added test case
This commit is contained in:
parent
bdd6041a5c
commit
352b23331b
1
AUTHORS
1
AUTHORS
@ -247,3 +247,4 @@ that much better:
|
|||||||
* Erdenezul Batmunkh (https://github.com/erdenezul)
|
* Erdenezul Batmunkh (https://github.com/erdenezul)
|
||||||
* Andy Yankovsky (https://github.com/werat)
|
* Andy Yankovsky (https://github.com/werat)
|
||||||
* Bastien Gérard (https://github.com/bagerard)
|
* Bastien Gérard (https://github.com/bagerard)
|
||||||
|
* Trevor Hall (https://github.com/tjhall13)
|
||||||
|
@ -345,7 +345,7 @@ def update(_doc_cls=None, **update):
|
|||||||
value = {key: {'$each': value}}
|
value = {key: {'$each': value}}
|
||||||
elif op in ('push', 'pushAll'):
|
elif op in ('push', 'pushAll'):
|
||||||
if parts[-1].isdigit():
|
if parts[-1].isdigit():
|
||||||
key = parts[0]
|
key = '.'.join(parts[0:-1])
|
||||||
position = int(parts[-1])
|
position = int(parts[-1])
|
||||||
# $position expects an iterable. If pushing a single value,
|
# $position expects an iterable. If pushing a single value,
|
||||||
# wrap it in a list.
|
# wrap it in a list.
|
||||||
|
@ -842,10 +842,16 @@ class InstanceTest(MongoDBTestCase):
|
|||||||
|
|
||||||
@requires_mongodb_gte_26
|
@requires_mongodb_gte_26
|
||||||
def test_modify_with_positional_push(self):
|
def test_modify_with_positional_push(self):
|
||||||
|
class Content(EmbeddedDocument):
|
||||||
|
keywords = ListField(StringField())
|
||||||
|
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
tags = ListField(StringField())
|
tags = ListField(StringField())
|
||||||
|
content = EmbeddedDocumentField(Content)
|
||||||
|
|
||||||
|
post = BlogPost.objects.create(
|
||||||
|
tags=['python'], content=Content(keywords=['ipsum']))
|
||||||
|
|
||||||
post = BlogPost.objects.create(tags=['python'])
|
|
||||||
self.assertEqual(post.tags, ['python'])
|
self.assertEqual(post.tags, ['python'])
|
||||||
post.modify(push__tags__0=['code', 'mongo'])
|
post.modify(push__tags__0=['code', 'mongo'])
|
||||||
self.assertEqual(post.tags, ['code', 'mongo', 'python'])
|
self.assertEqual(post.tags, ['code', 'mongo', 'python'])
|
||||||
@ -856,6 +862,16 @@ class InstanceTest(MongoDBTestCase):
|
|||||||
['code', 'mongo', 'python']
|
['code', 'mongo', 'python']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.assertEqual(post.content.keywords, ['ipsum'])
|
||||||
|
post.modify(push__content__keywords__0=['lorem'])
|
||||||
|
self.assertEqual(post.content.keywords, ['lorem', 'ipsum'])
|
||||||
|
|
||||||
|
# Assert same order of the list items is maintained in the db
|
||||||
|
self.assertEqual(
|
||||||
|
BlogPost._get_collection().find_one({'_id': post.pk})['content']['keywords'],
|
||||||
|
['lorem', 'ipsum']
|
||||||
|
)
|
||||||
|
|
||||||
def test_save(self):
|
def test_save(self):
|
||||||
"""Ensure that a document may be saved in the database."""
|
"""Ensure that a document may be saved in the database."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user