1
votes

I have a field last_update in which I want to store the current datetime as a user edits a form in form view. Basically I need to default the value of last_update to the system's date.

I tried using:

<field name="last_update" default_last_update="datetime.now()"/>

But, it is not working.

3
you want last_update date time right ? And you don't want to edit this date right? when this field you require ? - Bhoomi Vaishnani
Yes @BhoomiPatel - Mingg Lex

3 Answers

3
votes

In the python file:

from datetime import datetime

last_update = fields.Datetime(string='Last Update',default=lambda self: fields.datetime.now())
0
votes

In Every model, you will always have "write_date", which stores last record update time.

Still if you wants to add this field and update it every time when record update then you can set default when record create and inherit write() method to update every time when record update, set current time in that field as following :

last_update = fields.Datetime(string='Last Update',default=fields.Datetime.now)

@api.multi
def write(self, vals):
    vals.update({'last_update':fields.Datetime.now})
    return super(<your_class_name>, self).write(vals)
-2
votes

You Can Also Use This:

from datetime import datetime

    last_update = fields.Date(string='Last Update',default=datetime.now())