model
model装饰器的作用是返回一个集合列表定义。
@api.model
def_get_lang(self):
langs=self.env['res.lang'].search([])
return[(lang.code,lang.name)for lang in langs]
one
one装饰器的作用是对每一条记录都执行对应的方法,没有返回值。
@api.one
def_compute_now(self):
self.now=fields.Datetime.now()
-
Decorate a record-style method where 'self' is expected to be a singleton instance.
-
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.
-
In case the method is decorated with @returns, it concatenates the resulting instances. Such a method:
This decorator loops automatically on Records of RecordSet for you. Self is redefined as current record:
@api.one
def func(self):
self.name = 'xyz'
constrains
constrains装饰用于对字段进行限制定义
@api.constrains('age')
def_check_age(self):
if self.age<16:
raise ValueError(_('Age must be older than 16'))
mutil
self
' is a recordset. The method typically defines an operation on records. Such a method。
Self will be the current RecordSet without iteration. It is the default behavior:
@api.multi
def func(self):
len(self)
-
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.
-
It is always better use @api.multi with self.ensure_one() instead of @api.one to avoid the side effect in return values.