odoo 获取继承模型的计算字段

odoo get compute fields of inherit models

我写了一个 class 继承了 purchase.order,所以我想得到 "amount_untaxed" 的值是计算字段。 我试过这段代码:

class PurchaseOrderInherit(models.Model):
    _inherit='purchase.order'
    test_value=fields.Float(string='test value')

@api.model
def create(self,vals):
    vals['test_value']=self.amount_untaxed
    print(vals['test_value'])
    return super(PurchaseOrderInherit,self).create(vals)

但是打印功能return0.0。 请有人帮助我。

计算字段是在create调用中计算的,如果要获取值:

@api.model
def create(self,vals):
    # create returns the newly created record
    rec = super(PurchaseOrderInherit,self).create(vals)
    print(rec.amount_untaxed)
    # if you want to set the value just do this
    rec.test_value = rec.amount_untaxed  # this will trigger write call to update the field in database
    return rec

计算字段是即时计算的。在您调用 super 之前,self.amount_untaxed 的值将不可用。

@api.model
def create(self,vals):
    res = super(PurchaseOrderInherit,self).create(vals)
    print(res.test_value)
    return res  

如果 test_value 字段是一个计算字段,而不是在 createwrite 方法中计算它的值,您只需覆盖计算的相同方法 amount_untaxed 值。