【问题标题】:odoo get compute fields of inherit modelsodoo 获取继承模型的计算字段
【发布时间】:2019-12-23 12:02:45
【问题描述】:

我写了一个继承 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)

但是 Print 函数返回 0.0。 请有人帮助我。

【问题讨论】:

    标签: inheritance field odoo


    【解决方案1】:

    计算域是动态计算的。在您调用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 值。

    【讨论】:

      【解决方案2】:

      如果要获取值,则计算域在 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
      

      【讨论】:

        猜你喜欢
        • 2019-10-22
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        • 2023-01-18
        • 2018-07-14
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        相关资源
        最近更新 更多