Search code examples
datetimeodoo-11

How can I use the actual datetime for a field in Odoo 11


I want some like datetime.now() or similar in Odoo 11 to get the actual date and time.

I tried the code I saw in another answers in stawoverflow, but doesn´t works for me. Can someone put an example for how to do it?

Examples that doesn´t works:

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

exit = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())

exit= fields.Date.context_today(self, timestamp=datetime.datetime.now())

Solution

  • Use:

    default=fields.Datetime.now
    

    You can find an example at sale.py.

    The following explanation is taken from Odoo 11 ORM API

    class odoo.fields.Datetime(string=, **kwargs)
    ...

    For default values fields.datetime.now() should be used instead.

    Edit
    You can also use time module to set default values

    default=lambda *a: time.strftime('%Y-%m-%d')