是否由于后端列更改而覆盖Python Eve Endpoint?

时间:2019-06-03 14:51:24

标签: eve

如果要在到达数据库之前运行一些数据转换,如何覆盖端点。例如,假设我们有一个people表,其列名为fname,并将其重命名为first_name。但是我们的用户正在使用fname进行查询。有没有一种方法可以使用people的自定义路由覆盖端点,以便我可以将列名从fname转换为first_name,或更复杂的方法是,在调用SQLALchemy之前运行一些python代码自己还是将其返回到Eve框架以继续调用数据库?

例如使用快速入门,我尝试了类似的方法,但是没有用:

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管理此类数据库更改?

1 个答案:

答案 0 :(得分:1)

您是否看过Event Hooks,尤其是database event hooks?它们使您可以将标注函数挂钩到db事件(插入,替换,删除,获取)。例如,在函数内,您可以在传入有效负载到达数据库之前对其进行更改。

invalid query was interpolated in a join.
If you want to pass a query to a join, you must either:

  1. Make sure the query only has `where` conditions (which will be converted to ON clauses)
  2. Or wrap the query in a subquery by calling subquery(query)