从烧瓶应用程序添加标签到mongodb记录

时间:2012-07-12 20:21:05

标签: python mongodb post flask pymongo

我正在使用Flask和PyMongo创建一个爱好应用程序,以在浏览器中呈现mongodb记录。

这是维基百科编辑的数据库,记录如下:

{
    "_id" : ObjectId("4ff1c0d0dec94126a2000000"),
    "image" : "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Us_declaration_independence_signatures.jpg/350px-Us_declaration_independence_signatures.jpg",
    "unvandalism" : "<div>:<span class=\"diffchange diffchange-inline\">53</span>.  [[Arthur Middleton]]</div>], [<div>:<span class=\"diffchange diffchange-inline\">54</span>.  [[Button Gwinnett]]</div>], [<div>:<span class=\"diffchange diffchange-inline\">55</span>.  [[Lyman Hall]]</div>], [<div>:<span class=\"diffchange diffchange-inline\">56</span>.  [[George Walton]]</div>",
    "delta" : 0,
    "date" : ISODate("2012-07-02T00:44:57.781Z"),
    "offender" : "24.119.187.78",
    "reverter" : "Chillin411",
    "anon" : true,
    "vandalism" : "<div>:<span class=\"diffchange diffchange-inline\">54</span>.  [[Arthur Middleton]]</div>], [<div>:<span class=\"diffchange diffchange-inline\">55</span>.  [[Button Gwinnett]]</div>], [<div>:<span class=\"diffchange diffchange-inline\">56</span>.  [[Lyman Hall]]</div>], [<div>:<span class=\"diffchange diffchange-inline\">57</span>.  [[George Walton]]</div>",
    "time" : 1341189897781,
    "diff_url" : "http://en.wikipedia.org/w/index.php?diff=500257674&oldid=498121414",
    "date_str" : "Mon, 02 Jul 2012 00:44:57 GMT",
    "page" : "Signing of the United States Declaration of Independence"
}

我希望能够创建一个与记录相关联的文本字段,以允许用户向该记录添加标记。所以我知道它应该是使用模板中的某个表单的更新/ upsert POST操作。但是我在实现这一目标的基本方法上已经陷入困境,因为获取每条记录的上下文并说“在此处记录此记录并更新用户已提交标记并单击提交的标记”。我在想它。

在现有设置的基础上实现这一目标的最实用方法是什么?

1 个答案:

答案 0 :(得分:1)

不确定您是否正在寻找pymongo helpMongoDB helpFlask help

也许这段代码会帮助你...

from flask import request

tags = request.form.get('tags', []) # Where tags is a list of new tags

mongo_collection.update(
    {'_id': ObjectId('4ff1c0d0dec94126a2000000')},
    {'$pushAll': {'tags': tags}},
    )