python 前夕如何在存储到数据库之前处理数据
How to process data before storing to database in python eve
我目前正在学习 python eve 框架和 mongoDB 数据库以进行 restful API 开发。在前夕,基本的 CRUD 操作仅通过在 settings.py
文件中定义模式来完成。客户端可以发送 GET/POST 方法,数据会根据预定义的模式自动存储到 mongoDB。
如果我想在将数据插入 mongoDB 之前对其进行预处理怎么办(例如:客户端仅发送产品数量和价格,然后服务器计算总金额并将产品、价格和金额存储到数据库中).如果我想在回复客户之前处理我的数据怎么办?我们应该使用烧瓶控制器方法(像这样 EVE - define custom flask controllers)并手动将数据存储到数据库吗?
你在这里问了两件事。
首先,如果你想在响应GET请求之前操作已经存储的数据,你需要的是on_fetched_resource_<resource_name>
和on_fetched_item_<resource_name>
数据库事件挂钩。您可以在返回之前将您想要的信息添加到响应中:
When a GET, POST, PATCH, PUT, DELETE method has been executed, both a on_post_ and on_post__ event is raised. You can subscribe to these events with multiple callback functions. Callbacks will receive the resource accessed, original flask.request object and the response payload.
def post_get_callback(resource, request, payload):
print('A GET on the "%s" endpoint was just performed!' % resource)
def post_contacts_get_callback(request, payload):
print('A get on "contacts" was just performed!')
app = Eve()
app.on_post_GET += post_get_callback
app.on_post_GET_contacts += post_contacts_get_callback
app.run()
在此处查看文档:http://python-eve.org/features.html#post-request-event-hooks
但是如果您想在将数据存储到数据库之前处理 POST 数据,您需要的是 on_insert_<resource_name>
数据库事件挂钩。您可以在资源保存到数据库之前将您想要的信息添加到那里:
Database event hooks work like request event hooks. These events are fired before and after a database action. Here is an example of how events are configured:
def add_sum(items):
for item in items:
item['sum'] = item['a'] + item['b']
app = Eve()
app.on_insert_item += add_sum
我目前正在学习 python eve 框架和 mongoDB 数据库以进行 restful API 开发。在前夕,基本的 CRUD 操作仅通过在 settings.py
文件中定义模式来完成。客户端可以发送 GET/POST 方法,数据会根据预定义的模式自动存储到 mongoDB。
如果我想在将数据插入 mongoDB 之前对其进行预处理怎么办(例如:客户端仅发送产品数量和价格,然后服务器计算总金额并将产品、价格和金额存储到数据库中).如果我想在回复客户之前处理我的数据怎么办?我们应该使用烧瓶控制器方法(像这样 EVE - define custom flask controllers)并手动将数据存储到数据库吗?
你在这里问了两件事。
首先,如果你想在响应GET请求之前操作已经存储的数据,你需要的是on_fetched_resource_<resource_name>
和on_fetched_item_<resource_name>
数据库事件挂钩。您可以在返回之前将您想要的信息添加到响应中:
When a GET, POST, PATCH, PUT, DELETE method has been executed, both a on_post_ and on_post__ event is raised. You can subscribe to these events with multiple callback functions. Callbacks will receive the resource accessed, original flask.request object and the response payload.
def post_get_callback(resource, request, payload):
print('A GET on the "%s" endpoint was just performed!' % resource)
def post_contacts_get_callback(request, payload):
print('A get on "contacts" was just performed!')
app = Eve()
app.on_post_GET += post_get_callback
app.on_post_GET_contacts += post_contacts_get_callback
app.run()
在此处查看文档:http://python-eve.org/features.html#post-request-event-hooks
但是如果您想在将数据存储到数据库之前处理 POST 数据,您需要的是 on_insert_<resource_name>
数据库事件挂钩。您可以在资源保存到数据库之前将您想要的信息添加到那里:
Database event hooks work like request event hooks. These events are fired before and after a database action. Here is an example of how events are configured:
def add_sum(items):
for item in items:
item['sum'] = item['a'] + item['b']
app = Eve()
app.on_insert_item += add_sum