Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
2894 Visualizzazioni

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
Abbandona
Risposta migliore
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
Abbandona
Autore

Thank you so much! This worked perfectly

Post correlati Risposte Visualizzazioni Attività
5
dic 22
5567
2
ott 20
8361
1
nov 23
62401
2
feb 23
4257
1
gen 21
5411