Skip to Content
Menu
This question has been flagged
2 Replies
2708 Views

Managing our contact/res.partner structure with additional fields that should be inherited to all of their children and children of the children I stuck in the point how to this done in a clear way.

First approach was to think that an onchange function on the field of the record triggers the changes on the same field of his childs. And while the field on the childs are changed the same onchange function triggers there again and changes their childs and so on. But this approach showed that the onchange function on the child field is not triggered.

Next approach (#2) was to collect all records that are child, grandchilds ... of the actual record and set the new value to the field in each found child-record. But althought all records was found and said their field-value should change only the direct first level childs have the change done, all others in deeper levels of the tree staid on their old value.

Extending this approach (#3) by directly write the record-changes instead of setting the field-value brought that all childs got the new value like expected, but then they got the change also done if the user discards his changes by not saving the initial record.

So I really dont know which point in this I am thinking of wrong?


field1 = fields.Many2one('otherModel')

#approach1
@onchange('field1')
_def _onchange_function(self):
    for child in self.child_ids:
    child.field1 = self.field1

#approach2
@onchange('field1')
_def _onchange_function(self):
    # _get_all_childs() gives a recordset of alle childs/grandchilds ...
    for child in self._get_all_childs():
        child.field1 = self.field1

#approach3
@onchange('field1')
_def _onchange_function(self):
    # _get_all_childs() gives a recordset of alle childs/grandchilds ...
    for child in self._get_all_childs():
        child.write({'field1':self.field1.id})


Avatar
Discard
Author

there's a typing error in my examples, but cant edit the post: should be @api.onchange('field') instead of @onchange('field'); but this typing error was only in the post

Best Answer

Hello

Check below link will help you 

link

Avatar
Discard
Related Posts Replies Views Activity
3
Jul 24
27565
1
Oct 23
363
2
Oct 23
676
2
Aug 23
1963
4
Aug 23
18248