如何在django中生成mongodb objectid以插入具有id的嵌套对象

时间:2014-01-18 17:58:22

标签: django mongodb pymongo mongoengine objectid

我在django中使用mongoengine。我想创建一个objectid,我可以在公司集合的嵌套对象批发商中插入

class wholesaler(EmbeddedDocument):
    wid                 =   ObjectIdField()
    name                =   StringField(required = True,min_length = 3)

class firm(Document):
    name                = StringField(required=True)
    wholesalers         = ListField(EmbeddedDocumentField(wholesaler))

在View.py

wholesaler1 = wholesaler(wid = ??
                         ,name = '')

1 个答案:

答案 0 :(得分:2)

import bson
bson.objectid.ObjectId()
=> ObjectId('55dada4065d1ac2052207776')