当另一个模型的字段发生变化时,如何使@api.depends触发一个函数

时间:2018-04-19 19:52:50

标签: odoo-11

我是odoo dev中的新手,我正试图让'formula'字段发生变化(在marketplace.marketplace模型中)时字段值'recommanded_price'(来自marketplace.marketplaceinfo模型)改变

我使用了@ api.depends装饰器,但它没有触发函数

这是我的代码的基本要素

class marketplace(models.Model):
_name = 'marketplace.marketplace'
_description = 'Marketplace'

formula = fields.Char('Formula')
product_ids = fields.One2many('marketplace.marketplaceinfo', 'mkp', 'Products')

class marketplace_info(models.Model):
_name = 'marketplace.marketplaceinfo'
_description = 'Information about a product marketplace'
mkp = fields.Many2one('marketplace.marketplace','Marketplace',ondelete = 'cascade',required = True,
help='Marketplace of this product'
)
product_tmpl_id = fields.Many2one(
'product.template', 'Product Template',
index=True, ondelete='cascade')
price = fields.Float('Price', default=0.0, digits=dp.get_precision('Product Price'),
help="The selling price for a product within the marketplace")
recommonded_price = fields.Float('Recommended Price', compute='_compute_recommonded_price' ,default=0.0, digits=dp.get_precision('Product Price'),
readonly = True, help="The recommended selling price for a product within the marketplace")

@api.depends('mkp.formula')
def _compute_recommonded_price(self):
#do something

1 个答案:

答案 0 :(得分:0)

@ api.depends

如果装饰器中指定的任何字段被ORM更改或在表单中更改,则此装饰器将触发对装饰函数的调用:

@api.depends('name', 'an_other_field')
def afun(self):
pass

其他你可以使用另一个装饰

@ api.onchange

如果装饰器中指定的任何字段在表单中更改,则此装饰器将触发对装饰函数的调用:

@api.onchange('fieldx')
def do_stuff(self):
   if self.fieldx == x:
       self.fieldy = 'toto'

差异: 在上一个示例中,self对应于当前在表单上编辑的记录。在on_change上下文中,所有工作都在缓存中完成。因此,您可以在函数内部更改RecordSet,而无需担心更改数据库。这是@ api.depends

的主要区别

参考:enter link description here