2017-10-12 102 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) 

感謝,