2

For some reason, the remaining field is not updated when a new value is added ,why can this be?

yearly_pool = fields.Integer(string='Yearly pool')
working_days = fields.Integer(string='Wordkdays count', compute='_compute_working_days', store=True)
remaining = fields.Integer(string='Remaining', compute='_compute_remaining', store=True)
holidays_ids = fields.Many2many('hr.holidays', string='Freedoms allowed ', compute='_compute_holidays_ids')

Field methods:

remaining:

@api.multi
@api.depends('yearly_pool', 'working_days')
def _compute_remaining(self):
    for record in self:
        record.remaining = record.yearly_pool - record.working_days

working_days:

@api.multi
@api.depends('holidays_ids')
def _compute_working_days(self):
    for record in self:
        record.working_days = sum(record.holidays_ids.mapped('working_days'))

holiday_ids:

@api.multi
def _compute_holidays_ids(self):
    for record in self:
        record.holidays_ids = self.env['hr.holidays'].search([
            ('employee_id', '=', record.employee_id.id),
            ('year', '=', record.year),
            ('state', '=', 'validate'),
        ])
  • Does it update the fields for the newly created records? – Kenly Dec 14 '21 at 09:02
  • Could you explain your question a little bit? – SaltySteven Dec 14 '21 at 13:01
  • 1
    Of course. When you go to the form view and create a new record, does the fields which have `store=True` get computed or not? I tested your code after removing `multi` decorator and adding the ``depends`` decorator to the `holidays_ids` field (it depends on `employee_id` and `year` fields), the values are well computed when I create a new record – Kenly Dec 14 '21 at 13:09
  • It is not computed for some reason. i tried it as you described but it still doesn't work, the value still doesn't update. – SaltySteven Dec 14 '21 at 14:26
  • If store = True is in the working_days field, 0 is displayed, but if not, the correct value is displayed, but the remaining field is not computed. – SaltySteven Dec 14 '21 at 14:29
  • 1
    Try to recompute the values for old records – Kenly Dec 14 '21 at 14:31
  • Ok, but if I remove the store = True modifier for the remaining field, it works. Could this cause more trouble if the store = True modifier is not there? – SaltySteven Dec 14 '21 at 14:39
  • 1
    You can create a server action and call the compute methods. – Kenly Dec 14 '21 at 14:39
  • 1
    We add ``store=True`` when we want to use those fields in search domain or search methods that require the value to be stored in database. If you need only to show the fields in views, you can remove the ``store`` attribute – Kenly Dec 14 '21 at 14:42
  • Thank you very much for your help and explanation. I guess then the depends decorator doesn't need the remaining field either? – SaltySteven Dec 14 '21 at 14:54
  • You are welcome. Note that if the compute method uses the values of other fields, it should specify those fields using [depends()](https://www.odoo.com/documentation/11.0/reference/orm.html#computed-fields) – Kenly Dec 14 '21 at 15:08

0 Answers0