#Define the end date field. The end date is determined by the start date plus the duration. To set the end date, you need to calculate the duration yourself.
end_date = fields.Date(string="End Date", store=True,
compute='_get_end_date', inverse='_set_end_date')
#Response function for the "get end date" event: calculate the end date
@api.depends('start_date', 'duration')
def _get_end_date(self):
for r in self:
if not (r.start_date and r.duration):
r.end_date = r.start_date
continue
start = fields.Datetime.from_string(r.start_date)
duration = timedelta(days=r.duration, seconds=-1)
r.end_date = start + duration
#Response function for the "set end date" event: calculate and set the duration
def _set_end_date(self):
for r in self:
if not (r.start_date and r.end_date):
continue
start_date = fields.Datetime.from_string(r.start_date)
end_date = fields.Datetime.from_string(r.end_date)
r.duration = (end_date - start_date).days + 1Usage of inverse method attribute values in Odoo14 — reverse modifying dependent values based on compute field values
Usage of the Odoo14 inverse method attribute value
Sign in to leave a comment
