I have the following model:
class Comment(EmbeddedDocument):
comment = StringField()
comment_id = ObjectIdField()
class Blog(Document):
title = StringField(max_length=120, required=True)
content = StringField(max_length=500, required=True)
comments = ListField(EmbeddedDocumentField(Comment))
I need to be able to reference each comment. If I run the code below, the new comment will not have it's own _id field
comment = Comment(comment='New comment!!')
product.comments.append(comment)
product.save()
Hi, Embedded documents dont auto generate an id - so you need to provide one or set one as a default eg:
from bson import ObjectId
class Comment(EmbeddedDocument):
comment = StringField()
comment_id = ObjectIdField(default=ObjectId)
@rozza I know this is an old discussion here but I have a relevant question:
Is it possible to later query the embedded document similar to this
comment = Comment.objects.with_id(object_id='an_id_str')
or is it necessary to query from the parent class (Blog in this case)? I haven't been able to query the embedded document by id
Most helpful comment
Hi, Embedded documents dont auto generate an id - so you need to provide one or set one as a default eg: