Python Eve - 如果存在则更新记录,否则插入
Python Eve - Update Record if exists otherwise Insert
我正在使用默认示例..
run.py
from eve import Eve
app = Eve(template_folder=tmpl_dir)
if __name__ == '__main__':
app.run(debug=True)
settings.py
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PUT', 'PATCH', 'DELETE']
CACHE_CONTROL = 'max-age=20'
CACHE_EXPIRES = 20
IF_MATCH = False
people = {
# 'title' tag used in item links.
'item_title': 'person',
'item_url': 'regex("[a-zA-Z0-9.]+")',
'item_lookup': True,
'item_lookup_field': 'firstname',
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'firstname'
},
'schema': {
'firstname': {
'type': 'string',
#'required': True,
'unique': True,
},
'age': {
'type': 'integer'
}
}
}
DOMAIN = {
'people': people
}
现在,我可以通过
轻松创建新条目
curl -X POST -F "firstname=john" -F "age=24" "http://127.0.0.1:5000/people"
输出
{"_updated": "Fri, 02 Dec 2016 10:12:58 GMT", "_created": "Fri, 02 Dec 2016 10:12:58 GMT", "_status": "OK", "_id": "5841492a10cf9320678bef65", "_links": {"self": {"href": "people/5841492a10cf9320678bef65", "title": "person"}}}
我现在可以轻松发送 curl 请求
curl -X GET "http://127.0.0.1:5000/people/john"
并获取记录,因为 firstname 在我的附加查找中
输出
{"_updated": "Fri, 02 Dec 2016 10:12:58 GMT", "firstname": "john", "age": 24, "_links": {"self": {"href": "people/5841492a10cf9320678bef65", "title": "person"}, "collection": {"href": "people", "title": "people"}, "parent": {"href": "/", "title": "home"}}, "_created": "Fri, 02 Dec 2016 10:12:58 GMT", "_id": "5841492a10cf9320678bef65"}
我现在还可以修补文档并更改年龄,
curl -X PATCH -F "age=32" "http://127.0.0.1:5000/people/john"
输出
{"_updated": "Fri, 02 Dec 2016 10:15:56 GMT", "_created": "Fri, 02 Dec 2016 10:12:58 GMT", "_status": "OK", "_id": "5841492a10cf9320678bef65", "_links": {"self": {"href": "people/5841492a10cf9320678bef65", "title": "person"}}}
我的问题:
上面的 PATCH 仅在记录存在时有效,有没有办法我可以发送带有数据的 PUT 或 PATCH 请求,让 eve 决定创建新的如果条目已存在,则输入或修改?
希望我的问题够清楚
您使用 additional_lookup
设置的附加端点是只读的:
Besides the standard item endpoint which defaults to /<resource>/<ID_FIELD_value>
, you can optionally define a secondary, read-only, endpoint like /<resource>/<person_name>
.
在您的设置中,我认为您根本不需要设置 additional_lookup
,因为它只是重新定义 item_lookup_field
(它设置了您的 read/write 端点)。
我正在使用默认示例..
run.py
from eve import Eve
app = Eve(template_folder=tmpl_dir)
if __name__ == '__main__':
app.run(debug=True)
settings.py
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PUT', 'PATCH', 'DELETE']
CACHE_CONTROL = 'max-age=20'
CACHE_EXPIRES = 20
IF_MATCH = False
people = {
# 'title' tag used in item links.
'item_title': 'person',
'item_url': 'regex("[a-zA-Z0-9.]+")',
'item_lookup': True,
'item_lookup_field': 'firstname',
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'firstname'
},
'schema': {
'firstname': {
'type': 'string',
#'required': True,
'unique': True,
},
'age': {
'type': 'integer'
}
}
}
DOMAIN = {
'people': people
}
现在,我可以通过
轻松创建新条目curl -X POST -F "firstname=john" -F "age=24" "http://127.0.0.1:5000/people"
输出
{"_updated": "Fri, 02 Dec 2016 10:12:58 GMT", "_created": "Fri, 02 Dec 2016 10:12:58 GMT", "_status": "OK", "_id": "5841492a10cf9320678bef65", "_links": {"self": {"href": "people/5841492a10cf9320678bef65", "title": "person"}}}
我现在可以轻松发送 curl 请求
curl -X GET "http://127.0.0.1:5000/people/john"
并获取记录,因为 firstname 在我的附加查找中
输出
{"_updated": "Fri, 02 Dec 2016 10:12:58 GMT", "firstname": "john", "age": 24, "_links": {"self": {"href": "people/5841492a10cf9320678bef65", "title": "person"}, "collection": {"href": "people", "title": "people"}, "parent": {"href": "/", "title": "home"}}, "_created": "Fri, 02 Dec 2016 10:12:58 GMT", "_id": "5841492a10cf9320678bef65"}
我现在还可以修补文档并更改年龄,
curl -X PATCH -F "age=32" "http://127.0.0.1:5000/people/john"
输出
{"_updated": "Fri, 02 Dec 2016 10:15:56 GMT", "_created": "Fri, 02 Dec 2016 10:12:58 GMT", "_status": "OK", "_id": "5841492a10cf9320678bef65", "_links": {"self": {"href": "people/5841492a10cf9320678bef65", "title": "person"}}}
我的问题:
上面的 PATCH 仅在记录存在时有效,有没有办法我可以发送带有数据的 PUT 或 PATCH 请求,让 eve 决定创建新的如果条目已存在,则输入或修改?
希望我的问题够清楚
您使用 additional_lookup
设置的附加端点是只读的:
Besides the standard item endpoint which defaults to
/<resource>/<ID_FIELD_value>
, you can optionally define a secondary, read-only, endpoint like/<resource>/<person_name>
.
在您的设置中,我认为您根本不需要设置 additional_lookup
,因为它只是重新定义 item_lookup_field
(它设置了您的 read/write 端点)。