Community: Framework mailing list archives

expert-framework@mail.odoo.com

Add records to One2many field in @api.onchange function

by
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 
sale.order...

Here my code:

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

     @api.onchange('order_line')
     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 += line.new({'order_id': self.id, 
'product_id': add_opt_product.id, 'product_uom': add_opt_product.uom_id.id})

         self.order_line = order_lines

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

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

     @api.multi
     @api.depends('product_id')
     def _compute_opt_products(self):
         for line in self:
             product_variant_ids = self.env['product.product']
             for product_variant in 
line.product_id.product_tmpl_id.product_variant_ids:
                 for optional_product in 
product_variant.optional_product_ids:
                     product_variant_ids += 
optional_product.product_variant_ids

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



Best regards
Stefan Reisich
www.rove.de

-- 

Rove.design GmbH
Niedergasse 43
64319 Pfungstadt