由于后端列更改而覆盖 Python Eve 端点?
Override Python Eve Endpoint because of backend column changes?
如果我想在到达数据库之前运行 进行一些数据转换,我该如何覆盖端点。例如,假设我们有一个 people
table,列名 fname
,我们将其重命名为 first_name
。但是我们的用户正在使用 fname
进行查询。有没有一种方法可以使用 people
的自定义路由覆盖端点,以便我可以将列名称从 fname
转换为 first_name
或更复杂的 运行 一些 python 在我自己调用 SQLALchemy 或者可能将其返回到 Eve 框架以继续调用数据库之前的代码?
例如使用 QuickStart,我尝试了类似的方法,但没有成功:
from eve import Eve
from flask import jsonify
app = Eve()
@app.route('/people/<name>')
def custom_people_func(name):
return jsonify(name='Override', people_name=name)
if __name__ == '__main__':
app.run()
settings.py
people = {
# 'title' tag used in item links. Defaults to the resource title minus
# the final, plural 's' (works fine in most cases but not for 'people')
'item_title': 'person',
# by default the standard item entry point is defined as
# '/people/<ObjectId>'. We leave it untouched, and we also enable an
# additional read-only entry point. This way consumers can also perform
# GET requests at '/people/<lastname>'.
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'lastname'
},
# We choose to override global cache-control directives for this resource.
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
# most global settings can be overridden at resource level
'resource_methods': ['GET', 'POST'],
'schema': schema
}
DOMAIN = {'people': people}
当我curl -i http://127.0.0.1:5000/people/obama
时,它不会调用我定义的方法,而是调用默认的 Eve 路由。
一般来说,我们如何管理此类数据库更改,如果可能的话,使用 Eve?
你调查过 Event Hooks, specifically into database event hooks 了吗?它们允许您将标注函数挂接到您的数据库事件(插入、替换、删除、获取。)例如,在您的函数中,您可以在传入的有效负载到达数据库之前更改它。
>>> def before_insert(resource_name, items):
... print('About to store items to "%s" ' % resource)
... # modify incoming items here
>>> app = Eve()
>>> app.on_insert += before_insert
>>> app.run()
如果我想在到达数据库之前运行 进行一些数据转换,我该如何覆盖端点。例如,假设我们有一个 people
table,列名 fname
,我们将其重命名为 first_name
。但是我们的用户正在使用 fname
进行查询。有没有一种方法可以使用 people
的自定义路由覆盖端点,以便我可以将列名称从 fname
转换为 first_name
或更复杂的 运行 一些 python 在我自己调用 SQLALchemy 或者可能将其返回到 Eve 框架以继续调用数据库之前的代码?
例如使用 QuickStart,我尝试了类似的方法,但没有成功:
from eve import Eve
from flask import jsonify
app = Eve()
@app.route('/people/<name>')
def custom_people_func(name):
return jsonify(name='Override', people_name=name)
if __name__ == '__main__':
app.run()
settings.py
people = {
# 'title' tag used in item links. Defaults to the resource title minus
# the final, plural 's' (works fine in most cases but not for 'people')
'item_title': 'person',
# by default the standard item entry point is defined as
# '/people/<ObjectId>'. We leave it untouched, and we also enable an
# additional read-only entry point. This way consumers can also perform
# GET requests at '/people/<lastname>'.
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'lastname'
},
# We choose to override global cache-control directives for this resource.
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
# most global settings can be overridden at resource level
'resource_methods': ['GET', 'POST'],
'schema': schema
}
DOMAIN = {'people': people}
当我curl -i http://127.0.0.1:5000/people/obama
时,它不会调用我定义的方法,而是调用默认的 Eve 路由。
一般来说,我们如何管理此类数据库更改,如果可能的话,使用 Eve?
你调查过 Event Hooks, specifically into database event hooks 了吗?它们允许您将标注函数挂接到您的数据库事件(插入、替换、删除、获取。)例如,在您的函数中,您可以在传入的有效负载到达数据库之前更改它。
>>> def before_insert(resource_name, items):
... print('About to store items to "%s" ' % resource)
... # modify incoming items here
>>> app = Eve()
>>> app.on_insert += before_insert
>>> app.run()