1、constrains
该约束方法中参数:必须是非关系字段名,不支持关系字段,如 many2one 等
只有在 create()和 write()方法调用时,起作用
@api.constrains('name', 'description')
def _check_description(self):
for record in self:
if record.name == record.description:
raise ValidationError("Fields name and description must be different")
2、onchage
该方法返回一个伪记录,同样只支持非关系字段。
@api.onchange('partner_id')
def _onchange_partner(self):
self.message = "Dear %s" % (self.partner_id.name or "")
.. code-block:: python
return {
'domain': {'other_id': [('partner_id', '=', partner_id)]},
'warning': {'title': "Warning", 'message': "What is this?", 'type': 'notification'},
}
3、depends
该方法每个参数必须是一个字符串,可以是关系字段名 + 点 + 字段名
pname = fields.Char(compute='_compute_pname')
@api.depends('partner_id.name', 'partner_id.is_company')
def _compute_pname(self):
for record in self:
if record.partner_id.is_company:
record.pname = (record.partner_id.name or "").upper()
else:
record.pname = record.partner_id.name
4、depends_context
参数是上下文 context 中的 key 值
price = fields.Float(compute='_compute_product_price')
@api.depends_context('pricelist')
def _compute_product_price(self):
for product in self:
if product.env.context.get('pricelist'):
pricelist = self.env['product.pricelist'].browse(product.env.context['pricelist'])
else:
pricelist = self.env['product.pricelist'].get_default_pricelist()
product.price = pricelist.get_products_price(product).get(product.id, 0.0)
除了以下特殊依赖字段,所有依赖项必须是 hashable
- force_company (value in context or current company id),
- uid (current user id and superuser flag),
- active_test (value in env.context or value in field.context).
5、models
@api.model
def method(self, args):
...