Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
11 Odpovědi
74202 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit

Perfect..

Nejlepší odpověď

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
Zrušit

It works

Thanks a lot man .. :)

Nejlepší odpověď


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
Zrušit
Nejlepší odpověď

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
Zrušit
Autor

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

Nejlepší odpověď

 

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

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

to be:

fields.Datetime.now


Avatar
Zrušit
Nejlepší odpověď

For finding current time use below code.


import time

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

print "Current time is : ",local

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
3
čvn 25
1026
1
čvc 23
3725
4
pro 22
9661
2
dub 21
3719
1
říj 18
5077