Skip to Content
Menu
This question has been flagged
2 Replies
5830 Views

Hello,

I want to fill a date field by the date of today(current day) when moving from state to another ( ex from pending to done )

How can do that, thanks

Avatar
Discard
Best Answer

Hi there,

You can do this by using an @api.onchange function on the field you'd like to detect changes on. Then you can write on another field. A theoretical example:

import datetime

@api.onchange('state')
def change_date_field(self):
self.your_date_field = datetime.datetime.today().strftime('%Y-%m-%d')

Regards,
Yenthe

Avatar
Discard
Best Answer

Hi,

Suppose if you have got a function for changing the state of the record, either you can edit the original function or override the function using the super and do the necessary.


Suppose if this the function for changing the state of the record,

@api.multi
def button_done(self):
self.write({'state': 'done'})

and your date field is,

date = fields.Date('Date')


Now you can edit the original function and assign current date like this,

import datetime

@api.one
def button_confirm(self):
self.date = datetime.date.today()
self.state = 'confirm'


also, you can do it by overriding the function.


Thanks

Avatar
Discard

Why not @api.onchange? Better is also @api.multi with self.ensure_one() honestly.