背景信息
我上一个问题(In Eve, how can you make a sub-resource of a collection and keep the parent collections endpoint?)的答案是使用Eve的多个端点,这是Eve的一个数据源特性。在IRC频道中,我与cuibonobo交谈,她通过将game_id更改为一个对象而不是字符串来实现这一点,如下所示:
http://gist.github.com/uunsamp/d969116367181bb30731
然而,我没有做到这一点,正如您从对话中可以看到的那样,我将文档以不同的方式放入集合中:
14:59 < cuibonobo>编号。只是因为您以前的设置文件将游戏id保存为字符串,所以查找将无法工作 15:00 < cuibonobo> --它只适用于game_id已保存为ObjectId的文档 15:01 < cuibonobo>当前的工作方式,如果您将类型设置为' ObjectId ',它将在将字符串保存到数据库中之前将其转换为ObjectId。但是这种转换不会发生在字符串中 15:02 < znn>我还没有使用eve来存储对象 15:02 < znn>我一直在使用mongo接口插入条目 15:03 < cuibonobo>哦。嗯。这可能会使事情复杂化。Eve在插入文档之前确实会键入转换和其他内容。 15:04 < cuibonobo>,所以一般不建议直接把东西插入芒果
问题,它将我引向堆栈溢出:)
使用http方法POST和使用mongo将文档插入到集合中有什么区别?用户最终能够使用任何一种文档插入方法吗?
附加信息
在问这个问题之前,我一直在浏览http://github.com/nicolaiarocci/eve/blob/develop/eve/methods/post.py,但这可能需要一段时间才能理解,而不仅仅是询问一个可能比我更熟悉代码的人。
发布于 2014-10-04 06:40:31
快速的答案是Eve添加了几个元字段etag、updated、created以及每个存储的文档。如果您想在本地存储文档(而不是通过HTTP),可以使用post_internal
Intended for internal post calls, this method is not rate limited,
authentication is not checked and pre-request events are not raised.
Adds one or more documents to a resource. Each document is validated
against the domain schema. If validation passes the document is inserted
and ID_FIELD, LAST_UPDATED and DATE_CREATED along with a link to the
document are returned. If validation fails, a list of validation issues
is returned.使用example
from run import app
from eve.methods.post import post_internal
payload = {
"firstname": "Ray",
"lastname": "LaMontagne",
"role": ["contributor"]
}
with app.test_request_context():
x = post_internal('people', payload)
print(x)插入post_internal的文档将受到相同的验证,并将通过HTTP存储它们。在0.5dev(尚未发布)补丁中,还添加了PUT和DELETE内部方法。
https://stackoverflow.com/questions/26185829
复制相似问题