Python 2.7 Odoov10中向导的sales.order.line中的返回值

Python 2.7 Odoov10中向导的sales.order.line中的返回值,python-2.7,odoo-10,Python 2.7,Odoo 10,我在向导“pack_id”中有一个多个类型字段,在sale.order.line中也有一个多个“pack_id”类型字段。我希望向导'pack_id'的Many2many类型字段的值返回sale.order.line字段'pack_id' 为此,我的代码如下: 您可以通过以下方式更新代码: @api.multi def action_salepack_add(self): order_id = self._context.get('active_id',False)

我在向导“pack_id”中有一个多个类型字段,在sale.order.line中也有一个多个“pack_id”类型字段。我希望向导'pack_id'的Many2many类型字段的值返回sale.order.line字段'pack_id'

为此,我的代码如下:


您可以通过以下方式更新代码:

@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来创建值。这仅适用于many2one。如果该字段是一个2many或多个:

(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)

修正了错误。以下是错误的解决方案:

@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)

谢谢,

您好,product.product中的字段product\u pack是什么类型的?它是多个类型的字段它显示以下错误:ValueError:预期的单例:product.pack1,2,10。我也申请了loop。谢谢Mohamed Karara。。。!错误已修复。你好,穆罕默德,如何在Odoov10中的单个多个字段中添加多个字段值?我在此链接上的问题:
@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)