如果我有一个mongoengine文件,
class Blog(Document):
text = StringField()
tags = ListField(EmbeddedDocumentField(Tag))
class Tag(EmbeddedDocument):
tag = StringField()
如何编写django表单以验证和保存帖子数据
{
"text": "My first blog post",
"tags": [
{"tag":"mongo"}, {"tag":"django"}
]
}
答案 0 :(得分:2)
如果您使用mongoengine>=0.8
,那么:
blog = Blog.from_json('''{
"text": "My first blog post",
"tags": [
{"tag":"mongo"}, {"tag":"django"}
]
}''').save()
或者你已经有了dict:
blog = Blog._from_son({
"text": "My first blog post",
"tags": [
{"tag":"mongo"}, {"tag":"django"}
]
}).save()