I would like to append a new ListField EmbeddedDocument to an existing ListField EmbeddedDocument document. In other words appending a new document to list that belongs to a a document in list.
My Model: A Post can contain several Comments, each comment can have several Likes:
class Post(Document):
txt = StringField()
comments = ListField(EmbeddedDocumentField(Comment))
class Comment(EmbeddedDocument):
comment = StringField()
comment_id = ObjectIdField()
likes = ListField(EmbeddedDocumentField(Like))
class Like(EmbeddedDocument):
user = ReferenceField(User)
date = DateTimeField(default=datetime.utcnow,required=True)
My Code: (it's not working 'append' command dosen't exists, only 'set' exists)
def appendNewLike():
user = {..}
target = ObjectId(commentId)
newLike = Like(user=user)
Product.objects(comments__comment_id=target).update(append_comments__S__likes=newLike)
Ideal solution would be something like:
def appendNewLike():
user = {..}
target = ObjectId(commentId)
newLike = Like(user=user)
Product.objects(comments__comment_id=target).comments.likes.append(newLike)
Comments? Suggestions?