Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
2863 Zobrazení

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?


Avatar
Zrušit
Nejlepší odpověď
It seems the main problem is recursive onchage. we can solve it by adding context on XML fields like

<field name="urgent" context="{'toggle_urgent': True}"/>

class Item(models.Model):
    _name = 'my.item'                                                                          
urgent = fields.Boolean()
order_id = fields.Many2one('my.order')

class Order(models.Model):
    _name = 'my.order'                          
urgent = fields.Boolean()
item_ids= fields.One2Many('my.item', 'order_id')

​    @api.onchange('urgent', 'iteam_ids')
def _onchange_iteam_ids(self):
if self.env.context.get('toggle_urgent'):
self.item_ids.write({'urgent': self.urgent})
else:
self.urgent = any(self.item_ids.mapped('urgent'))
Avatar
Zrušit
Autor

Thank you so much! This worked perfectly

Related Posts Odpovědi Zobrazení Aktivita
5
pro 22
5537
2
říj 20
8324
1
lis 23
62302
2
úno 23
4175
1
led 21
5377