Odoo Help

2

1
11 Answers
5
Avatar

Janeesh

--Janeesh--

4375
| 6 3 8
Cochin, India
--Janeesh--

Odoo Developer

Janeesh
2017/09/24 6:35

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'),
}
7
Avatar
--deep--

Techno-Functional Associate with 7+ years of experience in Odoo (formerly known as OpenERP).

deep
2016/10/26 2:05

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
supreeth
2016/11/18 1:26

It works

Nebil Aydi
2018/01/12 4:57

Thanks a lot man .. :)

GLOBAL CREATIVE CONCEPTS TECH CO LTD (iWesabe)
2018/02/14 0:06

Good

3
Avatar

Ray Carnes

--Ray Carnes--
20941
| 10 7 11
Greater Los Angeles, United States
--Ray Carnes--

Business Analyst

OpenERP 6.1, 7.0; Odoo 8.0, 9.0, 10.0, 11.0 and 12.0.

Completed Odoo Functional and Technical Training.

Functional Areas:

  • CRM/Sales

  • Inventory

  • Manufacturing

  • Accounting

  • Purchasing

Major Skills:

  • Business Process Reengineering

  • Efficiency Consulting

  • User Needs and GAP Analysis;

  • Functional and Technical Design;

  • Prototyping and Proof of Concepts;

  • Requirements Specifications;

  • Agile Implementation;

  • Data Migration;

  • Configuration & Customization (UI and code);

  • Integration - Odoo and non Odoo Applications and Services;

  • Training and Knowledge Transfer;

  • Go Live Support;

  • Helpdesk;

  • Version Upgrades and Migration.

  • Accounting Expert.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

I am a skilled Communicator and I only take the serious things seriously!

Ray Carnes
2016/08/30 10:24

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
Selverine
2016/08/30 10:40

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)

Ray Carnes
2016/09/01 22:35

See my updated answer for v7

0
Harshit Agarwal
2019/07/16 7:04


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.



-2
Khaled Hamed
2017/09/29 0:54

 

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

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

to be:

fields.Datetime.now


-3
Neha Patel (nat)
2017/01/19 5:29

For finding current time use below code.


import time

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

print "Current time is : ",local

Ask a Question
Writer
Keep Informed
2 follower(s)
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register
Odoo Training Center

Access to our eLearning platform and experience all Odoo Apps through learning videos, use cases and quizzes.

Test it now