从另一个模型创建 stock.picking - Odoo v8

Creating stock.picking from another model - Odoo v8

我有这个方法:

@api.multi
def create_printy(self):
    copy_record = self.env['stock.picking'] # now you call the method directly
    for record in self:

        order_lines = []
        for rec in record.order_lines:
            order_lines.append(
            (0,0,
            {
                'product_id': rec.isbn.id,
                'product_qty': rec.qty,
                }
            ))
        copy_record.create({
            'origin': order.name,
            'picking_type_id.id': 'outgoing',
            'move_lines': order_lines, 
            'move_type': 'direct',
            'priority': '1',
            'company_id': record.company_id.id,
        })

这是一个按钮,可以根据我的模型创建一个新的 stock.picking

我试过 picking_type_id.id 但它似乎不起作用,在标准插件的每个示例中,你只看到 picking_type_id,而且,我没有任何定义模型,但我虽然可以传递其中一种可用类型,即 outgoing(我需要的类型)。

现在,它抛给我这个:

Integrity Error

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: picking_type_id - picking.type.id]

那么,我怎样才能将这个 picking_type_id 传递到 stock.picking,我是否应该在我的模型中定义这个字段,即使不需要?但是 stock.picking 模型需要它。

字段 picking_type_idstock.picking 模型中是必需的,您没有在 create 方法中指定它,而是为 picking_type_id.id 指定了一个值,这不是一个字段(Odoo 不会让您知道这个事实)。您必须找到选股类型对象的 ID,并将其传递给 create 方法。当您想要传出类型时,请使用此代码查找选择类型。如果您有多个仓库,则搜索方法会绑定到 return 多个记录,这就是我获取第一个的原因。但是如果你想要另一个,你将不得不向 search 方法传递更多的参数才能更简洁。所以试试这个:

@api.multi
def create_printy(self):
    copy_record = self.env['stock.picking'] # now you call the method directly
    for record in self:
        order_lines = []
        for rec in record.order_lines:
            order_lines.append(
            (0,0,
            {
                'product_id': rec.isbn.id,
                'product_qty': rec.qty,
                }
            ))

        sp_types = self.env['stock.picking.type'].search([
            ('code', '=', 'outgoing')
        ])
        if len(sp_types) > 0:
            copy_record.create({
                'origin': order.name,
                'picking_type_id': sp_types[0].id,
                'move_lines': order_lines, 
                'move_type': 'direct',
                'priority': '1',
                'company_id': record.company_id.id,
            })