'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 1:[1]

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

Solution 2:[2]

Either remove react-refresh/babel from babel config for test environment or just change the if condition and it should work.

if (process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "test") {...}

Edit: Updated the if to use and instead of or

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2