Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
11 Odpowiedzi
74188 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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'),
}
Awatar
Odrzuć

Perfect..

Najlepsza odpowiedź

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




Awatar
Odrzuć

It works

Thanks a lot man .. :)

Najlepsza odpowiedź


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.



Awatar
Odrzuć
Najlepsza odpowiedź

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)
Awatar
Odrzuć
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

Najlepsza odpowiedź

 

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

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

to be:

fields.Datetime.now


Awatar
Odrzuć
Najlepsza odpowiedź

For finding current time use below code.


import time

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

print "Current time is : ",local

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
3
cze 25
1009
1
lip 23
3699
4
gru 22
9649
2
kwi 21
3707
1
paź 18
5062