【问题标题】:How to create a JSON controller in odoo?如何在 odoo 中创建 JSON 控制器?
【发布时间】:2016-05-04 02:25:32
【问题描述】:

我在谷歌上搜索有关如何在 odoo 上创建 json 视图的文档,但发现的信息很少

我需要创建一个 json 视图以从 javascript 访问。

我正在尝试这样:

@http.route('/test/status/<name>', auth='public', type='json')
    def temp(self, name,**kwargs):

        return [{'status': 'test',
                 }]

Javascript 代码:

function check_status() {
    var name = $('#name').val();
    $.getJSON("/test/status/" + name +"/",
            function(data){
                var status = data.status;
                $('#msg').text(status);
    });
};

我收到以下错误:

<function temp at 0x7f1a8b7d5140>, /test/status/lego/: Function declared as capable of handling request of type 'json' but called with a request of type 'http'

请帮助我卡住了

[编辑答案@odoo_user2]

function json_function_name() {
        odoo.define('custom_webpage.my_js', function (require) {'use strict';
            var ajax = require('web.ajax');
            ajax.jsonRpc('/pa/get_models/' + variable_id, 'call', {}).then(function (data) {
                if (data.models == false) {
                } else {
                    // load models
                    for (var i = 0; i < data.models.length; i++) {
                        var opt = document.createElement('option');
                        opt.innerHTML = data.models[i][1];
                        opt.value = data.models[i][0];
                        sel_models.appendChild(opt);
                    }
                }
            });
        })
    }
}

这是我使用的javascript函数和控制器:

@http.route('/pa/get_models/<brand_id>', auth='none', type='json',website=True)
def get_models(self,brand_id**kwargs):
    cr = http.request._cr
    res = utils.get_models(cr,int(brand_id))
    return {'models': res,}

【问题讨论】:

  • 也许你应该使用'@json'而不是'@http'

标签: json openerp odoo-8


【解决方案1】:

目前我正在研究返回 json 的 python web 控制器。我给你举个小例子:

from openerp import http, _
from openerp.http import request
import json

class ClassName(http.Controller):
    @http.route('url', auth='user', type="json")
    def className(self, **kw):
        cr = request.cr
        context = request.context
        uid = request.uid
        user = request.env['res.users'].sudo().browse(uid)
        # now in kw you have all arguments from your post request
        # and finally when you will finish work with data and want to
        # return json return like that
        return json.dumps(res)
        # where res is dictionary

另外,我建议在 python web 控制器中进行输入验证更安全! 将此答案标记为正确答案,这就是您所需要的。 (我想这就是你需要的)

【讨论】:

  • Darchiashvill 你好,你能分享完整的例子吗?
【解决方案2】:

在 odoo8 中使用 openerp.jsonRpc 而不是 $.getJSON 在 odoo9 中使用 var ajax = require('web.ajax'); ajax.jsonRpc

【讨论】:

  • 好的,我这样做了,现在我得到了响应,但是我必须更改控制器,例如: class odoo_test(http.Controller): @http.route('/test/status/', auth='none', type='json') def temp(self, name,**kwargs): status = 'status test' status_p = 5 res = {"jsonrpc": "2.0", "method" : "call", "params": { 'status':status, 'status_p':status_p, }, "id": None, } return JsonRequest(res) 并得到 'dict' 对象没有属性 'session' 错误
  • 解决:我需要返回:res = {'status':status, 'status_p':status_p, } return res
  • @MarianoDAngelo 你创建 Json 控制器,可以分享代码吗?
【解决方案3】:

检查这个 JS 调用 [1]

openerp.jsonRpc('/website/check_gengo_set', 'call',[...]

及其控制器 [2]

@http.route('/website/check_gengo_set', type='json', auth='user', website=True)

这很有效,所以由于某种原因,您可能没有执行正确的 JSON 请求。

此处触发错误[3],并为此弹出:请求与类型不匹配。

请注意,jsonrpc 调用会执行此操作 [4]:

openerp.jsonRpc = function(url, fct_name, params, settings) {
    return genericJsonRpc(fct_name, params, function(data) {
        return $.ajax(url, _.extend({}, settings, {
            url: url,
            dataType: 'json',
            type: 'POST',
            data: JSON.stringify(data, date_to_utc),
            contentType: 'application/json'
        }));
    });
};

无论如何,如果您想使用该 JS,也许您可​​以尝试使用 type="http" 并自己转储 json。应该可以的。

[1]https://github.com/OCA/OCB/blob/8.0/addons/website_gengo/static/src/js/website_gengo.js#L42

[2]https://github.com/OCA/OCB/blob/8.0/addons/website_gengo/controllers/main.py#L21

[3]https://github.com/OCA/OCB/blob/8.0/openerp/http.py#L290

[4]https://github.com/OCA/OCB/blob/8.0/addons/web/static/src/js/openerpframework.js#L859

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    相关资源
    最近更新 更多