Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
14126 Widoki


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

Awatar
Odrzuć
Autor Najlepsza odpowiedź

Originally were posted here as answer.

Awatar
Odrzuć

@Temur the url is broken, do you still have the solution for this?

Powiązane posty Odpowiedzi Widoki Czynność
2
sty 23
5074
0
lut 21
3275
1
sie 23
411
1
wrz 21
20286
2
kwi 21
4230