Community: Framework mailing list archives

Add records to One2many field in @api.onchange function

Stefan Reisich
- 06/09/2016 05:28:33
Dear odoo community,

I'm trying to add the option to select an optional product when adding a 
product to the sale order. But now I'm desperate because it doesn't 
works. I need your help please.

The optional product selection works well, I can select optional 
products when adding a sale.order.line, but they are not added to the 

Here my code:

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

     def _onchange_order_line(self):
         order_lines = self.order_line

         for line in self.order_line:
             for add_opt_product in line.add_opt_products:
                 order_lines +={'order_id':, 
'product_id':, 'product_uom':})

         self.order_line = order_lines

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

     opt_products = fields.Many2many('product.product', 
     opt_products_count = fields.Integer(compute='_compute_opt_products')
     add_opt_products = fields.Many2many('product.product', 
string='Optional Products')

     def _compute_opt_products(self):
         for line in self:
             product_variant_ids = self.env['product.product']
             for product_variant in 
                 for optional_product in 
                     product_variant_ids += 

             line.opt_products = product_variant_ids
             line.opt_products_count = len(product_variant_ids)

Best regards
Stefan Reisich

-- GmbH
Niedergasse 43
64319 Pfungstadt