Search code examples
pythonodoo-12

Odoo: updating related field without changing it's original value


I am trying to update field price in model custom.sale.line that's related to field product_id.sell_price in model custom.price but without affecting the original value of the latter (in other way i want the cashier to able to change the value on the sales invoice but without changing the value of the product in the database), i'm wondering if there's any change to do so I tried making another field other_price that takes the value of price, but even with store=True it doesn't save the new value in the database and reverts to the original value if anyone have a solution for this problem i would be thankful

below is the code

class CustomSaleLine(models.Model):
    _name = 'custom.sale.line'
    _sql_constraints = [
        ('product_br_uniq', 'unique(order_id, product_id)',
         'Cannot add a product that already exists')]
    product_id = fields.Many2one(comodel_name="custom.product", string="Product",
                                 domain="[('branch_line.branch_id.user_lines.user_id','=', user_id)]")
    sell_price = fields.Float(string='Main Price', related='product_id.sell_price', required=True, )
    other_price = fields.Float(string='Other Price', compute='_compute_price', store=True)
store=True, )

    @api.depends('sell_price')
    def _compute_price(self):
        for rec in self:
            rec.other_price = rec.sell_price


Solution

  • Try

    sell_price = fields.Float(string='Main Price', inverse='_compute_dummy', compute='_compute_price', required=True, store=True)
    
    @api.depends('product_id.sell_price')
    def _compute_price(self):
        for rec in self:
            rec.sell_price = rec.product_id.sell_price
            
    def _compute_dummy(self):
        pass