Search code examples
odooodoo-15

How to set unit_price automatically as I select product in odoo 15?


I have made one model which is listed below, I want to set the price automatically as I select the product.

class JobCardLine(models.Model):
    _name = "job.card.line"

    product_id = fields.Many2one('product.template', string="Product", tracking=True)    
    price = fields.Many2one('product.template.',string="Price", tracking=True)

I think it can be done using depends on onchange but not able to do that.


Solution

  • You can use on_change to automatically set the price to the product list_price

    Example:

    @api.onchange('product_id')
    def _change_price(self):
        self.price = self.product_id.list_price
    

    You will need to change the price field type to Float

    You can do it using a computed field but you will need to implement the inverse function to allow setting values on the field and the unit price should depend on product price