odoo newbie
odoo newbie

Reputation: 177

How to edit a computed field [Odoo 15]

I want reservation_amount to be the half of sale_total, but it has to be an editable field, so i can change it whenever i want. My problem now is when I change the value of the fields, it resets to half sale_total, I supposed it's because of the computed value. How can I do it to change it? Or how can i set as the first value of reservation_amount, the half of sale_total so It can be changed then?

Here is my actual code

sale_total = fields.Float(
    string="Total Sale", compute='_onchange_calculate_total')
reservation_amount = fields.Float(
    string="Reservation amount", readonly=False,
    store=True, compute="_onchange_reservation_amount")

@api.onchange('sale_total')
def _onchange_reservation_amount(self):
    for rec in self:
        rec.reservation_amount = rec.sale_total / 2

@api.onchange('reservation_line')
def _onchange_calculate_total(self):
    count = 0
    for line in self.reservation_line:
        count += line.subtotal

    self.sale_total = count

Upvotes: 2

Views: 715

Answers (1)

holydragon
holydragon

Reputation: 6728

Remove compute="_onchange_reservation_amount" out of the reservation_amount and you will be able to edit the field as you wish without the field getting reset. It will only reset to half of the sale_total when sale_total is changed.

Upvotes: 1

Related Questions