odoo api装饰器使用

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

返回一个记录集,有返回值。

Decorate a record-style method where ' 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)



mutil和one
  1. 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.

  2. It is always better use @api.multi with self.ensure_one() instead of @api.one to avoid the side effect in return values.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值