Skip to Content
Menu
This question has been flagged
5 Replies
1526 Views

Hello guys ,

I have have two fields product_uom_qty and x_unitparcarton that i created it in product module and i added it in sale module 

I created a third field x_totalunite and i'm trying to make it calculate product_uom_qty * x_unitparcarton

 I tried this but it didn't help 

 @api.onchange('product_uom_qty', 'x_unitparcarton')
def onchange_field(self):
if self.product_uom_qty or self.x_unitparcarton:
self.x_totalunite = self.product_uom_qty * self.x_unitparcarton

Avatar
Discard
Author Best Answer

I found another solution 

in database structure -> fields 

i created x_totalunite

and in calcul i added 

for record in self: 

 record['x_totalunite'] = self.product_uom_qty * self.x_unitparcarton

Avatar
Discard
Best Answer

onchange is normaly used for changes in UI. Rather use depends and make the field compute.

x_totalunite = fields.Float(compute='_get_value', store=True)
@api.depends('product_uom_qty', 'x_unitparcarton')
@api.multi
def _get_value(self):
   for rec in self:
      if rec.product_uom_qty and rec.x_unitparcarton:
          rec.x_totlaunite = rec.product_uom_qty * rec.x_unitparcartron                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

I hope the fields are in the same model else it can't work. Then you'll need to use related fields.
Avatar
Discard
Author

x_unitparcarton is a related field and it is working just fine

i still got 0 in x_totalunite which mean it's still not working

Best Answer

Try like this,
@api.v8 
@api.onchange('product_uom_qty', 'x_unitparcarton')
def onchange_field(self):
if self.product_uom_qty or self.x_unitparcarton:
    self.x_totalunite = self.product_uom_qty * self.x_unitparcarton

Avatar
Discard
Author

still the same problem