如何使用django表单将发布请求保存到mongoengine?

时间:2013-07-30 22:53:22

标签: django django-forms mongoengine

如果我有一个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"}
    ]
}

1 个答案:

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