Skip to main content

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.