我需要在Odoo上创建一个自定义控制器,以从特定任务中获取信息。而且我也可以产生结果。但是现在我面临一个问题。
客户需要检索具有特定字段的信息。
例如,客户端需要使用跟踪号来检索信息,并且数据也必须为JSON格式。如果跟踪号为15556456356,则网址应为www.customurl.com/dataset/ 15556456356
该URL的路由应为@http.route('/dataset/<string: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/<string: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'
。不要忘记将文件导入__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
:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句