3

I'm using odoo 10 i inherited from module stock.quant i add a new attribute and a new method. i want my method execute after each creating a new object in stock.quant. This is my code Thanks for help

class stock_quant(models.Model):
    _inherit = 'stock.quant'
    inventory_value_charge = fields.Float('Total Value',store=True,compute='update_stock_value')

    @api.one
    @api.depends('qty')
    def update_stock_value(self):
        stock_price_obj = self.env['stock.price.partition'].search([('id', '!=', False)])
        val_obj = stock_price_obj.search([('reception.pack_operation_product_ids.pack_lot_ids.lot_id.id', '=', self.lot_id.id)])
        if val_obj!= False:
            val_obj.calccule_price()
        else:
            self.inventory_value_charge=self.inventory_value
        #stock_price_obj = self.env['stock.price.partition'].search([('reception.pack_operation_product_ids.pack_lot_ids.lot_id.id', '=', self.lot_id.id)])
        return True

    @api.model
    def create(self, vals):
        res = super(stock_quant, self).create(vals)
        self.update_stock_value()
        return res
MOHAMED
  • 101
  • 2
  • 10

2 Answers2

2

Just change your create function like this

@api.model
def create(self, vals):
    res = super(stock_quant, self).create(vals)
    res.update_stock_value()
    return res
Kenly
  • 24,317
  • 7
  • 44
  • 60
sfx
  • 1,793
  • 17
  • 24
2

First of all the field is computed you don't have to call the method because it's allready called in the process of create call. and in write call too but only if you change the qty field.

So remove the create method you are just calling it for the second time.

Now make sure that your method is called by adding some print calls if you don't know how to use debug in you IDE.

Make sure you import the module in your __init__ files.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40