Ravi Singh
Ravi Singh

Reputation: 315

How to use Onchange with date field in odoo?

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

Upvotes: 2

Views: 1026

Answers (2)

Sayooj A O
Sayooj A O

Reputation: 211

@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

Upvotes: 2

Thayif kabir
Thayif kabir

Reputation: 726

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.

Upvotes: 2

Related Questions