我正在使用eve-API来访问MongoDB。但是,如果我想insert into select
项目(文档),我会收到405错误。
我可以使用curl访问我想要更新的文档,如下所示:
insert into user_points_table
select user_id, 500-sum(points_balance)
from `user_points_table`
where user_id = 74
group by user_id
having SUM(points_balance) >= 400 && SUM(points_balance) <= 499;
API返回所需的文档:
PATCH
我尝试使用curl来通过curl 127.0.0.1:5000/simulations/20151223_011329620
请求更新文档,如下所示:
{
"_updated": "Wed, 23 Dec 2015 00:13:29 GMT",
"regenerator": "565e12c58b724d7884cd02bb",
"identifier": "20151223_011329620",
"_etag": "9c50633f1bf34bcefb84237ce2477066529f3c0e",
"_links": {
"parent": {
"title": "home",
"href": "/"
},
"self": {
"title": "Simulation",
"href": "simulations/5679e72904c8880421b0abfa"
},
"collection": {
"title": "simulations",
"href": "simulations"
}
},
"_created": "Wed, 23 Dec 2015 00:13:29 GMT",
"status": "pending",
"_id": "5679e72904c8880421b0abfa",
"sectors": ["565e12c58b724d7884cd02b9", "565e12c58b724d7884cd02ba"]
}
由于这给了我一个405错误,我读了docs告诉我应该提供有效的PATCH
。因此我尝试了:
curl -X PATCH -d '{"status": "pending"}' http://127.0.0.1:5000/simulations/20151223_011329620
这给了我同样的405错误:
etag
我不知道为什么我不被允许curl -H "If-Match: 9c50633f1bf34bcefb84237ce2477066529f3c0e" -H "Content-Type: application/json" -X PATCH http://127.0.0.1:5000/simulations/20151223_011329620 -d '{"status": "pending"}'
所需的项目(文档),因为全局配置如下:
{"_status": "ERR", "_error": {"code": 405, "message": "The method is not allowed for the requested URL."}}
根据docs,我可以使用小写等效项PATCH
覆盖这些全局设置
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']
)在端点的定义中。因此,这些似乎都没有默认值,也没有在端点的定义中指定那些(resource_methods
下面的代码段)我找不到这个错误的原因。
item_methods
答案 0 :(得分:2)
默认情况下,只有_id字段可用于PATCH。如果你想使用其他字段进行PATCH,那么你需要在该配置中声明一个ID_FIELD
命名字段,该字段将指向配置中的其他字段,例如你的情况下的“标识符”。
编辑:
ID_FIELD
:用于唯一标识数据库中资源项的字段的名称。您希望在数据库上正确索引此字段。可以通过资源设置覆盖。默认为_id。
有关详细信息,请在此处阅读如何在settings.py文件中设置ID_FIELD:http://python-eve.org/config.html