콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
3128 화면

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?


아바타
취소
베스트 답변
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'))
아바타
취소
작성자

Thank you so much! This worked perfectly

관련 게시물 답글 화면 활동
5
12월 22
5867
2
10월 20
8677
1
11월 23
62793
2
2월 23
4632
1
1월 21
5724