何时在odoo中使用api.one和api.multi开瓶器? [英] When to use api.one and api.multi in odoo | openerp?
问题描述
最近发布了odoo(以前称为OpenERP)V8.在新的API方法中,引入了装饰器.在models.py
方法中需要用@api.one
或@api.multi
装饰.
Recently odoo (formerly OpenERP) V8 has been released. In new API method decorators are introduced. in models.py
methods needs to be decorated with @api.one
or @api.multi
.
引用 odoo文档我无法确定确切的用途.谁能详细解释.
Referring odoo documentation i can not determine the exact use. Can anybody explain in detail.
谢谢.
推荐答案
通常,两个精化程序都用于装饰记录样式的方法,其中'self
'包含记录集.让我简要说明何时使用@api.one
和@api.multi
:
Generally both decoarators are used to decorate a record-style method where 'self
' contains recordset(s). Let me explain in brief when to use @api.one
and @api.multi
:
1. @api.one
:
-
装饰一个记录样式的方法,其中" self "应为单例实例.
修饰的方法自动在记录上循环(即,对于记录集中的每个记录调用该方法),并创建一个包含结果的列表.
The decorated method automatically loops on records (i.e, for each record in recordset it calls the method), and makes a list with the results.
如果该方法使用@returns修饰,则它将连接所得到的实例.这样的方法:
In case the method is decorated with @returns, it concatenates the resulting instances. Such a method:
@ api.one def方法(self,args): 返回self.name
@api.one def method(self, args): return self.name
可以用记录样式和传统样式来调用,例如:
may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
names = recs.method(args)
names = model.method(cr, uid, ids, args, context=context)
- 每次自我"都被重新定义为当前记录.
-
装饰一个记录样式的方法,其中"
self
"是一个记录集.该方法通常定义对记录的操作.这样的方法: Decorate a record-style method where '
self
' is a recordset. The method typically defines an operation on records. Such a method:
2. @api.multi
:
@ api.multi def方法(自己,参数):
@api.multi def method(self, args):
可以用记录样式和传统样式来调用,例如::
may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
recs.method(args)
model.method(cr, uid, ids, args, context=context)
何时使用:
-
如果使用@ api.one,则返回的值在列表中. 网络客户端并不总是支持此功能,例如按键动作 方法. 在这种情况下,您应该使用@ api.multi装饰您的方法,并可能在其中调用self.ensure_one() 方法定义.
If you are using @api.one, the returned value is in a list. This is not always supported by the web client, e.g. on button action methods. In that case, you should use @api.multi to decorate your method, and probably call self.ensure_one() in the method definition.
始终最好将@ api.multi与self.ensure_one()而不是@ api.one一起使用,以避免在返回值中产生副作用.
It is always better use @api.multi with self.ensure_one() instead of @api.one to avoid the side effect in return values.
这篇关于何时在odoo中使用api.one和api.multi开瓶器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!