oulai kenny
oulai kenny

Reputation: 43

odoo get compute fields of inherit models

I have write a class that inherit of purchase.order,so i want to get the value of the "amount_untaxed" which is a computed field. I have try this code :

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)

But the Print function return 0.0. Please someone helps me.

Upvotes: 1

Views: 1191

Answers (2)

Kenly
Kenly

Reputation: 26708

A computed field is computed on-the-fly. The value of self.amount_untaxed will not be available until you call super.

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

If test_value field is a computed field, instead of calculating its value in create and write methods you have just to override the same method that calculates amount_untaxed value.

Upvotes: 1

Charif DZ
Charif DZ

Reputation: 14721

The computed field are computed in the create call, if you want to get the value:

@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

Upvotes: 0

Related Questions