Search code examples
odoo-8odooodoo-9

More than 1 product to dict


Here I adding product_id to vals dict but problem is that if there is more than 1 product, how can I add product_id of each line to this dict vals. Probably I need to make a list of product_ids and then add it to dict but struggling a bit

@api.multi
    def button_details(self):

        domain = [
            ('product_tmpl_id', '=', self.product_id.id)
        ]
        vals = {}

        bom_products = self.env['mrp.bom'].search(domain)
             for bom_line in bom_products.bom_line_ids:
                  vals['product_id'] = bom_line.product_id.id
                  vals['product_uom_qty'] = bom_line.product_qty

    context = self.env.context.copy()
    context['view_buttons'] = True
    view_id = self.env.ref('config.view_order_line_form_view').id
    view = {
        'name': _('Details'),
        'view_type': 'form',
        'view_mode': 'tree, form',
        'res_model': 'sale.order.line',
        'views' : [(view_id,'tree')],
        'type': 'ir.actions.act_window',
        'target': 'new',
        'readonly': True,
   #     'res_id': ,
        'context': context
    }
    return view

Updated the ultimate goal is to open a view that triggered by button with products and qty that i'm trying to extract from bom.line


Solution

    • Please change this code may it's help you.
        bom_products = self.env['mrp.bom'].search(domain)
    
        vals['product_id'] = bom_products.bom_line_ids.mapped('product_id.id')
    
    • And remove for loop it's not necessary.