Mongoengine: ListField EmbeddedDocumentField dosen't generate own _id field

Created on 2 Oct 2012  路  2Comments  路  Source: MongoEngine/mongoengine

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()

Most helpful comment

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)

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gareth-lloyd picture gareth-lloyd  路  26Comments

GroverChouT picture GroverChouT  路  36Comments

aisbaa picture aisbaa  路  14Comments

rezakamalifard picture rezakamalifard  路  23Comments

bagerard picture bagerard  路  14Comments