Navi
Navi

Reputation: 1052

How to assign current date to a date field in odoo 10

How to show current date before clicking the date field in odoo?

Upvotes: 1

Views: 13061

Answers (2)

Navi
Navi

Reputation: 1052

I found it.It is Simple, just write this on your python code like:

date = fields.Datetime(string="Date", default=lambda *a: datetime.now(),required=True)

or

like this

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

or

like this

date = fields.Date(default=fields.Date.today)

Upvotes: 2

CZoellner
CZoellner

Reputation: 14768

Odoo Date field class provides methods to get default values for like today.

For dates the method is called context_today() and for datetimes context_timestamp(). You are able to pass a timestamp to this methods to either get today/now (without timestamp) or a timestamp which will be formed by the logged in users timezone.

Code Example:

from odoo import fields, models


class MyModel(models.Model):
    _name = 'my.model'

    def _default_my_date(self):
        return fields.Date.context_today(self)

    my_date = fields.Date(string='My Date', default=_default_my_date)

Or the lambda version:

    my_date = fields.Date(
        string='My Date', default=lambda s: fields.Date.context_today(s))

Upvotes: 5

Related Questions