我需要在Odoo上创建一个自定义控制器,以从特定任务中获取信息。而且我也可以得出结果。但是现在我面临一个问题。
客户端需要检索具有特定字段的信息。
例如,客户端需要使用跟踪号来检索信息,并且数据也必须为JSON格式。如果跟踪号为15556456356,则网址应为www.customurl.com/dataset / 15556456356
该URL的路由应为@http.route('/dataset/<int:tracking_number>', type='http or json', auth="user or public")
,基本上方法应该是这样的:
import json
from odoo import http
from odoo.http import Response, request
class tracking(http.Controller):
# if user must be authenticated use auth="user"
@http.route('/dataset/<int:tracking_number>', type='http', auth="public")
def tracking(self, tracking_number): # use the same variable name
result = # compute the result with the given tracking_number and the result should be a dict to pass it json.dumps
return Response(json.dumps(result), content_type='application/json;charset=utf-8',status=200)
此方法接受http
请求并返回json
响应,如果客户端正在发送json
请求,则应更改type='json'
。不要忘记import __init___.py
中的文件。
让我们举个例子,我想通过在sale.order
中输入ID
来返回有关URL
的一些信息:
import json
from odoo import http
from odoo.http import Response, request
class Tracking(http.Controller):
@http.route('/dataset/<int:sale_id>', type='http', auth="public")
def tracking(self, sale_id):
# get the information using the SUPER USER
result = request.env['sale.order'].sudo().browse([sale_id]).read(['name', 'date_order'])
return Response(json.dumps(result), content_type='application/json;charset=utf-8',status=200)
因此,当我使用浏览器输入此URL时:http://localhost:8069/dataset/1
: