From 34646a414c15b016a10bbc95f4c1bd33baa88b97 Mon Sep 17 00:00:00 2001 From: Adam Parrish Date: Thu, 10 Nov 2011 12:03:51 -0800 Subject: [PATCH] Fixes bug using positional operator to update embedded documents. append_field wasn't getting reset to True in the loop, so fields wouldn't get appended to clean_fields after str was encountered [#354] --- AUTHORS | 1 + docs/changelog.rst | 1 + mongoengine/queryset.py | 2 +- tests/queryset.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 9ebc0054..4f68be2f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -73,4 +73,5 @@ that much better: * Julien Rebetez * Marc Tamlyn * Karim Allah + * Adam Parrish diff --git a/docs/changelog.rst b/docs/changelog.rst index dbb4a728..53943127 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Fixed positional operator when replacing embedded documents - Added Non-Django Style choices back (you can have either) - Fixed __repr__ of a sliced queryset - Added recursive validation error of documents / complex fields diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 8ebebe3a..6f01b765 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -1276,8 +1276,8 @@ class QuerySet(object): parts = [] cleaned_fields = [] - append_field = True for field in fields: + append_field = True if isinstance(field, str): # Convert the S operator to $ if field == 'S': diff --git a/tests/queryset.py b/tests/queryset.py index 5f5e78be..60adae37 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -378,6 +378,34 @@ class QuerySetTest(unittest.TestCase): self.assertRaises(OperationError, update_nested) Simple.drop_collection() + def test_update_using_positional_operator_embedded_document(self): + """Ensure that the embedded documents can be updated using the positional + operator.""" + + class Vote(EmbeddedDocument): + score = IntField() + + class Comment(EmbeddedDocument): + by = StringField() + votes = EmbeddedDocumentField(Vote) + + class BlogPost(Document): + title = StringField() + comments = ListField(EmbeddedDocumentField(Comment)) + + BlogPost.drop_collection() + + c1 = Comment(by="joe", votes=Vote(score=3)) + c2 = Comment(by="jane", votes=Vote(score=7)) + + BlogPost(title="ABC", comments=[c1, c2]).save() + + BlogPost.objects(comments__by="joe").update(set__comments__S__votes=Vote(score=4)) + + post = BlogPost.objects.first() + self.assertEquals(post.comments[0].by, 'joe') + self.assertEquals(post.comments[0].votes.score, 4) + def test_mapfield_update(self): """Ensure that the MapField can be updated.""" class Member(EmbeddedDocument):