2017-10-12 51 views
0

我在向导('pack_ids')中有一个Many2many类型字段,并且在sale.order.line中有一个Many2many('pack_id')类型字段。我希望Many2many类型的向导('pack_ids')字段的值在sale.order.line字段('pack_id')中返回。Odoov10中的向导在sales.order.line中的返回值

因为我这个代码是在这里:

class SalePackWizard(models.TransientModel): 
    _name = "sale.pack.wizard" 
    _description = "Sale Pack Wizard" 

    @api.onchange('product_id') 
    def _onchange_product_pack_name(self): 
     print"A:", self.product_id.product_pack 
     res = self.product_id.product_pack 
     a = {} 
     print "res:", res 
     if res: 
      domain = {'pack_ids': [('id', 'in', [v.id for v in res])]} 
      a= res 
      print "a:", a 
      return {'domain': domain} 

    product_id = fields.Many2one('product.product', string="Product Pack", required=True, domain="[('is_pack','=',True)]") 
    qty = fields.Float(string='Quantity', digits=dp.get_precision('Product Unit of Measure'), required=True, default=1.0) 

    pack_ids = fields.Many2many('product.pack', string='Pack Products', change_default=True, 
           default=_onchange_product_pack_name) 


    @api.multi 
    def action_salepack_add(self): 
     rec = self._context.get('active_ids', []) 
     print "REC", rec, self.product_id.categ_id #product_uom 
     if rec: 
      line_values = {'product_id': self.product_id.id, 
          #'design_id':self.design_id.id, 
          'pack_id': self.product_id.product_pack, 
          'category_id':self.product_id.categ_id.id, 
          'order_id':rec[0], 
          'product_uom_qty':self.qty, 
          } 
      sale_order_line = self.env['sale.order.line'].create(line_values) 
+0

嗨,知道这是什么领域** ** product_pack在** product.product类型**? –

+0

这是Many2one类型字段 –

回答

1

您可以通过更新代码:

@api.multi 
    def action_salepack_add(self): 
     order_id = self._context.get('active_id',False) 

     if order_id: 
      line_values = {'product_id': self.product_id.id, 
          'pack_id': [ (6, 0, [self.product_id.product_pack.id]) ], 
          'category_id':self.product_id.categ_id.id, 
          'order_id':order_id, 
          'product_uom_qty':self.qty, 
          } 
      sale_order_line = self.env['sale.order.line'].create(line_values) 

你不能创建一个many2many字段值只是付出的ID(此只为许多人)。如果该字段是一个或多个字段:

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary 
(1, ID, { values }) update the linked record with id = ID (write values on it) 
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well) 
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself) 
(4, ID) link to existing record with id = ID (adds a relationship) 
(5) unlink all (like using (3,ID) for all linked records) 
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs) 
+0

它显示以下错误:“ValueError:期望的单例:product.pack(1,2,10)”。我也申请了循环。 –

+0

您确定** product_pack **是many2one? –

+0

如果可能,您可以向我显示更多错误。 –

0

错误已修复。下面是错误的解决方案:

@api.multi 
    def action_salepack_add(self): 
     rec = self._context.get('active_ids', []) 
     print "REC", rec, self.product_id.categ_id #product_uom 
     if rec: 
      line_values = {'product_id': self.product_id.id, 
          #'design_id':self.design_id.id, 
          'pack_id': [(6, 0, [v.id for v in self.product_id.product_pack])], 
          'category_id':self.product_id.categ_id.id, 
          'order_id':rec[0], 
          'product_uom_qty':self.qty, 
          } 
      sale_order_line = self.env['sale.order.line'].create(line_values) 

感谢,