Help

2

fields.datetime.now() show the time of the latest restart - not the current time

Avatar
Selverine

Hi everyone,

I have a field datetime. This field should have by default the datetime of "now", the current time.

However, the default date is the time of the lastest restart.

Please find below my code:

'date_action': fields.datetime('Date current action', required=False, readonly=False, select=True),

_defaults = {
    'date_action': fields.datetime.now(),


Someones has an idea why? And how to solve it?

Many thanks,


Selverine

Avatar
Discard
6 Answers
6
Avatar
Janeesh
Best Answer

You have to use lambda function to get the current time. Otherwise it will take the latest server restart time.

_defaults = {
'date_action': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
}
Avatar
Discard
8
Avatar
deep
Best Answer

In V8 or above, if the class definition is as per latest api, i.e (models.Model)

then use the below like syntax, no need to import any date-related files.


date_action = fields.Datetime('Date current action', required=False, readonly=False, select=True
                                , default=lambda self: fields.datetime.now())




3 Comments
Avatar
Discard
Avatar
supreeth
-

It works

Avatar
Nebil Aydi
-

Thanks a lot man .. :)

Good

3
Avatar
Ray Carnes
Best Answer

For v8/v9: You have not defined the field correctly, not using a CAPITAL for Datetime:


'date_action': fields.Datetime('Date current action', required=False, readonly=False, select=True),


For v7: You are mixing the API functions.

Try:

from openerp.osv import fields, osv
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT

date.today().strftime(DEFAULT_SERVER_DATE_FORMAT)
2 Comments
Avatar
Discard
Avatar
Selverine
-

Hi Ray. Thank you for your answer. I tried with Capital for Datetime but I have the error: AttributeError: 'module' object has no attribute 'Datetime'. Do I need to import a specific class? PS: I am using OpenERP 7 ( sorry to forgot to mention it)

Avatar
Ray Carnes
-

See my updated answer for v7

2
Avatar
Harshit Agarwal
Best Answer


The reason I can figure out is, all the models start loading as soon as the server restart. For the default fields, while using datetime.now(), it takes the time as soon as the server restart and gets treated like a constant afterward. To overcome this, we can use either of the two methods given below:-

Method 1: Using a lambda function "lambda self: fields.datetime.now()"

In the new API, you can define the field as 

fields.datetime('Date current action', default=lambda self: fields.datetime.now(), required=False, readonly=False, select=True)

Method 2: Using "fields.Datetime.now"


fields.datetime('Date current action', default=fields.Datetime.now, required=False, readonly=False, select=True)

I preferred Method 2 over Method 1 as it seemed more promising and is already used in ERP base code.

Note: Please take care of the DateTime spelling (case-sensitive) in both the methods.

Hope this helps.



Avatar
Discard
-1
Avatar
Khaled Hamed
Best Answer

 

\Stack\ overflow\ link\ \,\ answer\ \#4\ solved\ the\ issue\ \\\

\
Just remove () from fields.Datetime.now()

to be:

fields.Datetime.now


Avatar
Discard
-3
Avatar
Neha Patel (nat)
Best Answer

For finding current time use below code.


import time

local = time.asctime(time.localtime())

print "Current time is : ",local

Avatar
Discard