Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How to access to the orm in Javascript ?

By
PY
on 2/17/15, 11:32 AM 1,241 views

Hi ! I'm on Odoo (v8).

I try to add some Javascript in my Odoo website.

Here's a sample of JS:

$(document).ready(function () {
    var foobar = new openerp.Model("foo.bar");
    var ids = foobar.call("get_dummy_ids", 1);
});

Here's where I include it:

<template id="my_module.assets_frontend" name="Website assets">
    <script type="text/javascript" src="/my_module/static/src/js/my_js.js"></script>
</template>

And here's the error:

Uncaught Error: Not session specified

Anyone have messed up with this issue ?


Update:

I tried this js code:

new openerp.Model(new openerp.Session(), 'res.users').call('search')

But it says:

"Server application error" Object { message: "Odoo Session Expired", code: 100, data: Object }
declare/genericJsonRpc/result<() ceba8ad:1120
.Deferred/promise.then/</</<() ceba8ad:249
jQuery.Callbacks/fire() ceba8ad:243
jQuery.Callbacks/self.fireWith() ceba8ad:248
done() ceba8ad:682
.send/callback()

Since I try to call it when no user is logged in, I kind of get it. But then, is there a way to query the model when no user is logged in ?

2
PY
On 9/17/15, 5:20 AM

Thanks to Neda Zilinskas, which pointed me in the right direction.

With a visitor's session (unregistered user), you can't call the openerp.Model functions it seems.

However, I was able to successfully call a controller function, via it's route ('/something'), like that:

var s = new openerp.Session();
s.rpc('/some/route', {
foo: bar,
}).then(function(result) {
console.log(result);
});


2
Nedas Zilinskas
On 5/1/15, 4:42 PM

Hi PY,

Looks like an old post, but I will still post an answer for the record.

Before creating a model you need to create a communication with the server session and pass a session to the model constructor as following.

 $(document).ready(function () {
    var session = new openerp.Session();
    var foobar = new openerp.Model(session, "foo.bar");
    var ids = foobar.call("get_dummy_ids", 1);
});


Old question, but I needed this answer now ^^ I will try that, thanks !

PY
on 9/17/15, 4:42 AM

Updated my question

PY
on 9/17/15, 5:01 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/17/15, 11:32 AM
Seen: 1241 times
Last updated: 9/17/15, 5:20 AM