Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a JSON controller in odoo?

Tags:

json

odoo

odoo-8

I'm googling docs on how to create a json view on odoo, but find litle info

I need to create a json view to access from javascript.

I'm trying like this:

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

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

Javascript code:

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

I'm getting the following error:

<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'

Please help i'm stuck

[edit for answer @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);
                    }
                }
            });
        })
    }
}

this is the javascript function I use, and the controller:

@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,}
like image 932
Mariano DAngelo Avatar asked Mar 25 '26 02:03

Mariano DAngelo


1 Answers

Currently I'm working on python web controller which returns json. I'll give you little example:

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

Also I recommend to make input validation in python web controller its more secure! Mark this answer as correct answer it this is what you need. (I think this is what you need)

like image 61
Dachi Darchiashvili Avatar answered Mar 26 '26 18:03

Dachi Darchiashvili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!