Lalit patidar
Lalit patidar

Reputation: 91

@onchange in odoo is not working properly

I have two models main.channel and sub.channel

main.channel has many2many relationship with sub.channel

sale.order has many2many relationship with both main.channel and sub.channel

Now in sale order I want that if I select a main.channel record then all records in sub.channel object that belongs to selected main.channel record should be fetched automatically.

I have done this functionality, but after fetching records when I click on save button, all child(sub.channel) records seems missing and not saved.

I have tried this by many different ways but I have not found proper solution for this yet

Below is my code that I have written in sale.order.

@api.onchange("main_channel_ids")
def sub_ids_values(self):
    for rec in self:
        sub_channels_list=[]
        for main_channel in rec.main_channel_ids:
            for sub_channel in main_channel.sub_channel_ids:
                sub_channels_list.append(sub_channel.id)
        #rec.sub_channel_ids = [(6,0,sub_channels_list)] #this is also not working
        rec..write({'sub_channel_ids': [(6, 0, sub_channels_list)]})   

Upvotes: 1

Views: 223

Answers (1)

You can write as following :

@api.onchange("main_channel_ids")
def sub_ids_values(self):
    for rec in self:
        sub_channels = rec.main_channel_ids.mapped('sub_channel_ids')
        rec.sub_channel_ids = sub_channels

Upvotes: 1

Related Questions