0

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.

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • `price = fields.Float(string="Price", related='product_id.lst_price')` replace the price field with the above why price is Many2one ? – Muhammad Yusuf May 19 '22 at 11:55

1 Answers1

2

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

Kenly
  • 24,317
  • 7
  • 44
  • 60