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

Hello Community,
I am writing some code that changes the value of a field in all the lines of a sales order.  So if a user changed the category_id in one sale order line, other lines should be change this categ_id as well.

But for some reason this is not working. The values on other lines are not updated and stay the same like nothing happened. 

class SaleOrderLine(models.Model):
_inherit="sale.order.line"

@api.onchange('categ_id')
def _onchange_categ_id(self):
    
    for changed_line in self:
            #find all lines of the parent sale_order
             all_lines = changed_line.order_id.order_line
            vals = {'categ_id': changed_line.categ_id}
            for line in all_lines:
                line.update(vals)
    return {}

-> update to give more clarification on what the reason behind this code is.

The reason for this that, in the bigger picture, is that I can create groups of sale order lines, separated by line_sections. Each group of lines has the same categ_id, so if the categ_id of a section changes, all the lines in this section should change to the categ_id as well.

All the help is welcome.

Greetings,

Avatar
Discard

In that case I think it's not possible,

In the ORM API documentation,

It is not possible for a one2many or many2many field to modify itself via onchange. This is a webclient limitation - see #2693.

https://www.odoo.com/documentation/13.0/reference/orm.html

https://github.com/odoo/odoo/issues/2693

Author

I think you're right. That is a pity, I will have to look for an other solution.

can you change your remark to an answer so we can set that as the right answer? Thanks again for the help.

Author Best Answer

Hello, I found a solution for this problem.

I changed the onchange function that happens in the line of a One2many so it sets a flag that it is changed.

Then I added a onchange function on the "order_line"-field in the order model. This onchange method is triggered as well after a line changed and it is triggered the last in line after a change in the records.

In the onchange-orderline function I check the states of the flags, reset them and preform the necessary action using a function call on the sale object. (don't know if it is necessary to do it with a function call, but I saw that in sale.order.line onchange fiscal postition)

That does the job. The lines are changed on-screen!

class SaleOrderLine(models.Model):
    _inherit="sale.order.line"

    categ_changed = fields.Boolean("technical field, do not use or show to user", default = False)

    @api.onchange('categ_id')
    def _onchange_categ_id(self)
        for line in self:
            line.categ_changed = True
        return True

    def change_categs(self, changed_lines_lst)
        for line in self
            if self.id in changed_lines_lst:
                self.categ_changed = False
                #do necessary update actions within this line
        return

class SaleOrder(models.Model):
    _inherit="sale.order"

    @api.onchange('order_line')
    def _onchange_order_line(self)
        #we need to get this from te context because object might not be the same as screen values.
        ctx_lines = self.env.context.get('order_line')
        changed_lines_lst = []
        if ctx_lines:
            for ctx_line in ctx_lines:
                if ctx_line[2]:
                    if ctx_line[2].get("categ_changed")
                        changed_lines_lst.append(ctx_line[1])

           for order in self:
                self.order_line.change_categs(changed_lines_lst)

adding context to view:
<xpath expr="//field[@name='order_line']" position="attributes">
<attribute
name="context">{"order_line": order_line}</attribute>
</xpath>


Good luck :-)
Avatar
Discard
Best Answer

Update:

In that case I think it's not possible,

In the ORM API documentation,

It is not possible for a one2many or many2many field to modify itself via onchange. This is a webclient limitation - see #2693.

https://www.odoo.com/documentation/13.0/reference/orm.html

https://github.com/odoo/odoo/issues/2693


If I understand correctly,

You are trying to change a field name 'categ_id' in model 'sale.order', and you modify this field from a view in model 'sale.order', likely from view_id 'sale.view_order_form'

The expected behavior is that all 'order_line' records will have a corresponding field to be changed to the same.

If so, you don't need to use onchange, I use a Char field named 'categ_idd' as an example

class SaleOrder(models.Model): 
    _inherit = 'sale.order'
    categ_idd = fields.Char('Categ IDD')

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'
    categ_idd = fields.Char('Categ IDD', related='order_id.categ_idd')
Avatar
Discard
Author

Hello, this is not really what I am looking for. It should be that categ_id is a field of the sale.order.line and if the field categ_id is changed on one line it should change on other sale.order.lines as well.

The reason for this that, in the bigger picture, i can create groups of sale order lines, separated by line_sections. Each group of lines has the same categ_id, so if the categ_id of a section changes, all the lines in this section should change as well to the same categ_id. -> I will add this explanation to my question as well this might clarify my question more. Thank you for your answer.

Related Posts Replies Views Activity
1
Dec 21
5399
1
Apr 21
1386
2
Dec 20
3519
1
Jun 21
1550
1
May 24
5608