This question has been flagged
11 Replies
68289 Views

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
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

Perfect..

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())




Avatar
Discard

It works

Thanks a lot man .. :)

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
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)
Avatar
Discard
Author

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)

See my updated answer for v7

Best Answer

 

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

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

to be:

fields.Datetime.now


Avatar
Discard
Best Answer

For finding current time use below code.


import time

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

print "Current time is : ",local

Avatar
Discard