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

RPC with new v8 API (Call Python function from JavaScript examples) Odoo 8

By
Temur
on 5/27/15, 4:31 AM 2,483 views


in the new API, if you've to deal with ids/recordsets then for python function definition choose decorator:

  • @api.multi -to get recordset in your function

  • @api.one -to get browse_records one by one in your function

in examples I'll use @api.multi but @api.one also may be used to deal with ids, depending on requirements.

if it's simple function that does not have to deal with records/ids then for python function choose decorator:

  • @api.model -Allows to be polite with old style api

  • @api.multi -you can use it as well, just pass [ ] (empty array) as first argument in javascript...


an example model:

class my_model(models.Model):
_name = "my.model"

name = fields.Char('Name')

@api.multi
def foo_manipulate_records_1(self):
""" function returns list of tuples (id,name) """
return [(i.id,i.name) for i in self]

@api.multi
def foo_manipulate_records_2(self, arg1, arg2)
#here you can take advantage of "self" recordset and same tie use aditional arguments "arg1", "arg2"
pass

@api.model
def bar_no_deal_with_ids(self, arg1, arg2):
""" concatenate arg1 and arg2 """
return unicode(arg1) + unicode(arg2)


Odoo RPC examples:

assume that "list_of_ids" variable contains list(array) of ids of existing records of my.model model.

- call of function foo_manipulate_records_1 decorated with @api.multi:

new instance.web.Model("my.model")
.call( "foo_manipulate_records_1", [list_of_ids])
.then(function (result) {
// do something with result
});

-call of function foo_manipulate_records_2 decorated with @api.multi:

new instance.web.Model("my.model")
.call( "foo_manipulate_records_2", [list_of_ids, arg1, arg2])
.then(function (result) {
// do something with result
});

-call of function bar_no_deal_with_ids decorated with @api.model:

new instance.web.Model("my.model")
.call( "bar_no_deal_with_ids", [arg1, arg2])
.then(function (result) {
// do something with result
});


also if it has some sense depending on implementation, then you can call function decorated with @api.multi even if you have not to deal with ids (just pass empty array in place of ids, as firs element of argument list):

new instance.web.Model("my.model")
.call( "foo_manipulate_records_2", [[], arg1, arg2])
.then(function (result) {
// do something with result
});

this way may be useful in some cases, as undecorated function in v8.0 api is considered as @api.multi (as @api.multi is a default decorator)


except of two parameters to RPC call that I've used in examples (the function name and argument list), you can use third parameter -a dictionary of keyword arguments. it's highly recommended to turn around the context, as it may change behavior of remote procedure (localization, etc.). below I'll rewrite the last example with context argument in rpc call (same may be applied to all examples)

var self = this;
new instance.web.Model("my.model")
.call("foo_manipulate_records_2", [[], arg1, arg2], {'context':self.session.user_context})
.then(function (result) {
// do something with result
});

(of course you can use custom context as well instead of turning around the existing one, if that's a requirement.)


references: Odoo RPC documentation, Odoo new API method decorators

1

Temur

--Temur--
2894
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 5/27/15, 4:34 AM

Originally were posted here as answer.

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

2 follower(s)

Stats

Asked: 5/27/15, 4:31 AM
Seen: 2483 times
Last updated: 10/9/15, 11:14 AM