首页 > Windows开发 > 详细

odoo 使用restfultAPI调用模型中的方法

时间:2020-04-09 09:01:50      阅读:180      评论:0      收藏:0      [点我收藏+]

模块中的说明文档没有提怎样使用API调用模型中的方法,,这里根据代码做一下讲解:

代码如下,路由设置如下:

_routes = ["/api/<model>", "/api/<model>/<id>", "/api/<model>/<id>/<action>"]
    @validate_token
    @http.route(_routes, type="http", auth="none", methods=["PATCH"], csrf=False)
    def patch(self, model=None, id=None, action=None, **payload):
        """."""
        try:
            _id = int(id)
        except Exception as e:
            return invalid_response(
                "invalid object id", "invalid literal %s for id with base " % id
            )
        try:
            record = request.env[model].sudo().search([("id", "=", _id)])
            _callable = action in [
                method for method in dir(record) if callable(getattr(record, method))
            ]
            if record and _callable:
                # action is a dynamic variable.
                getattr(record, action)()
            else:
                return invalid_response(
                    "missing_record",
                    "record object with id %s could not be found or %s object has no method %s"
                    % (_id, model, action),
                    404,
                )
        except Exception as e:
            return invalid_response("exception", e, 503)
        else:
            return valid_response("record %s has been successfully patched" % record.id)

从中可以看到,需要使用PATCH请求才能实现调用,并且url地址的格式:/api///

odoo 使用restfultAPI调用模型中的方法

原文:https://www.cnblogs.com/qianxunman/p/12664105.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!