如何设置Eve资源和项调用以通过自定义的反序列化函数

2024-10-06 14:28:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法创建API以JSON格式返回datefields。所以我必须创建一个函数来序列化它。dump_datetime函数可以帮我完成这个任务。 我现在的问题是,每当我抛出GET resource请求时,我不知道如何让我的资源调用运行dump_datetime调用。在

假设有一个资源用户具有“birthdate”属性。如何使“birthdate”属性通过dump_datetime()函数?在

Since in Eve you could already get the item call by  http://127.0.0.1:5000/users/id

如何设置get调用以通过dump_datetime?在

我的问题清楚了吗?在

下面是一个使用我创建的反序列化函数的自定义函数的示例。我只想知道如何在Eve项目和资源调用中使用它。在

^{pr2}$

更新: 我应该编辑/var/www/healthyoptions middleware/eve/methods吗/获取.py对需要序列化的属性执行序列化函数?????在


Tags: 函数apijsongetdatetime属性序列化格式
1条回答
网友
1楼 · 发布于 2024-10-06 14:28:00

如果我理解你的要求,你只需要使用事件钩子。GET方法上的post请求钩子和您想要的资源应该可以完成这个任务;例如on_post_GET_<resource_name> = deserializing_function。或者,您也可以挂接数据库请求,例如在提取的资源上。在

根据文件:

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.

Database event hooks work like request event hooks. These events are fired before and after a database action.

http://python-eve.org/features.html#event-hooks

相关问题 更多 >