Search code examples
odoo-8odooodoo-10odoo-9

How do I use the on_change method to calculate total current value


How do I make a value change in real time after I input a specific field value in a form? e.g from the screenshot below , if I enter Quantity recieved as 10000 the Actual stock should compute to 80500.

so far this is the code for the on_change method I came up with : I would like to know whether this is the correct approach

@api.one
@api.onchange('qnty_recieved', 'init_stock')
def _compute_current_stock(self):

    qnty_recieved = self.qnty_recieved
    init_stock = self.init_stock
    current_quantity = self.current_quantity

    self.current_quantity = self.qnty_recieved + self.init_stock

Below is a screenshot of what I am trying to achieve.

enter image description here


Solution

  • If i'm not wrong you want to change your actual stock in real time based on quantity received field.

    This can be best achieved by using depends method.

    @api.one
    @api.depends('qnty_recieved')
    def _compute_current_stock(self):
    
        # Assuming current_quantity as the field name of actual stock
        self.current_quantity += self.qnty_recieved
    

    You should also add compute=_compute_current_stock, store=True keyword arguments to your actual stock field.