I am using Odoo v13. I need help with setting the order's urgent field to True if any of the urgent fields in its items are True. And also if you change the order's value, to set all of its items' values to it, so in essence the order's checkbox will be acting as a "Select All/ Deselect All" button, but you can also check the boxes individually.
class Order(models.Model):
urgent = fields.Boolean()
items = fields.One2many('my_module.items', inverse_name='order', ondelete='set null')class Item(models.Model):
urgent = fields.Boolean()
order= fields.Many2one('my_module.order')
I have tested a lot with compute, inverse and onchange but I have not achieved the desired effect. Inverse function runs on save button, so I had to replace it with onchange. But I had a weird interaction where the compute and inverse functions were being called twice with each change, while my code was supposed to only run one of the functions per change, once.
Now the trouble I'm having is that I can't modify the order's value from an onchange in the items. Simply nothing happens. I read that in the past you couldn't do this, but the last comment says it was fixed.
https://github.com/odoo/odoo/issues/2693
This is what I currently have:
# class Order(models.Model):
@api.onchange('urgent')
def select_all(self):
if not self.env.context.get('no_onchange'):
self.items.write({'urgent': self.urgent}) # This works fine# class Item(models.Model):
@api.onchange('urgent')
def any_urgent(self):
urgents = self.order.items.filtered(lambda m: m.urgent)
self.with_context(no_onchange=True).order.urgent = bool(len(urgents)) # This doesn't do anything.
Is there a feasible way to achieve what I'm trying to do?