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]
This commit is contained in:
Adam Parrish 2011-11-10 12:03:51 -08:00 committed by Ross Lawley
parent 5aeee9deb2
commit 34646a414c
4 changed files with 31 additions and 1 deletions

View File

@ -73,4 +73,5 @@ that much better:
* Julien Rebetez
* Marc Tamlyn
* Karim Allah
* Adam Parrish

View File

@ -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

View File

@ -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':

View File

@ -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):