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

模块中的说明文档没有提怎样使用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/ / /

猜你喜欢

转载自www.cnblogs.com/qianxunman/p/12664105.html