Set Default Date Field as Today or Any Date in Odoo

Date and datetime fields are among the most commonly used fields in business applications. While they may seem straightforward at first, these fields can actually be quite complex. Misusing them can lead to invisible yet painful bugs that can be difficult to track down and fix. This snippet aims to provide Odoo developers with a way to set default value for date tiem field.

//today
end_date = fields.Date(string='End Date', default=fields.Date.context_today, required=True)

//from external source
end_date = fields.Date(string='End Date', default=fields.Date.to_date(self._context.get('date_from')), required=false)

//any date
import time
start_date = fields.Date(string="Start Date", default=time.strftime('%Y-05-10'))

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close