简体   繁体   English

在 Eve 的预置回调中从数据库中获取项目的信息

[英]Get info of an item from database in pre-put callback in Eve

I have items with subId and some other fields stored in mongoDB.我有带有subId项目和存储在 mongoDB 中的其他一些字段。 Before the PUT request is submitted, I want to check if the subId which is sent in the header of the PUT request is equal to the subId of the desired item which is stored in mongoDB.在之前PUT请求被提交,我要检查,如果subId这是在标题中传送PUT请求等于subId存储在MongoDB中所需的项目。 So, I decided to use pre_put_callback to do this check.所以,我决定使用pre_put_callback来做这个检查。 Now I can get the subId from header of the request in this function, but I don't know how to access the subId of the item from mongoDB whose _id is provided by the user as the path of the request in pre_put_callback function.现在,我可以得到subId在此功能的请求头,但我不知道如何访问subId从MongoDB的,其项目的_id是由用户提供的作为请求的路径pre_put_callback功能。 Is there a way to access the subId of that item (which is stored in MongoDB) from pre_put_callback function?有没有办法从pre_put_callback函数访问该项目的subId (存储在 MongoDB 中)?

You can access an item of MongoDB database from a pre-request event hook such as pre_put_callback using current_app of flask package.可以从预先请求事件钩访问的MongoDB数据库的项目,例如pre_put_callback使用current_appflask包。 For example for pre_PUT event hook it can be accessed as below:例如对于 pre_PUT 事件挂钩,它可以按如下方式访问:

from flask import current_app as app
from eve import Eve

def pre_put_callback(resource, request, lookup):
    resource_db = app.data.driver.db[resource]
    item = resource_db.find_one(lookup)
    print("DB Item: ", item)

app = Eve()
app.on_pre_PUT += pre_put_callback

app.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM