How to use Onchange with date field in odoo?

Viewed 854

field declaration

    price=fields.Integer(string="Price")
    service_date=fields.Date(string="Last servicing date")
    service_charge=fields.Integer(string="Last Service Charge")
    total_charge=fields.Integer(string="Total Spent")

onchange Function in which servie_date is used as argument

    @api.onchange('service_date')
    def _onchange_total_charge(self):
        if self.total_charge > 0:
            self.total_charge+=self.service_charge
        else:
            self.total_charge=self.price+self.service_charge
2 Answers

I've used your code it works on my odoo instance. Please make sure that you call the same in field in your xml file. you can also use compute field to get your calculation done.

@api.onchange('service_date')
@api.depends('service_date')
def _onchange_total_charge(self):
    if self.total_charge > 0:
        self.total_charge += self.service_charge
    else:
        self.total_charge = self.price + self.service_charge

Try to re-write the code like this

Related