Skip to main content
 首页 » 编程设计

python之从另一个模型将 move_lines 传递到 stock.picking之Odoo v8

2024年10月01日7dflying

我有这个方法:

@api.multi 
def create_printy(self): 
    copy_record = self.env['stock.picking']  
    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': record.name, 
                'picking_type_id': sp_types[0].id, 
                'move_lines': order_lines,  
                'move_type': 'direct', 
                'priority': '1', 
                'company_id': record.company_id.id, 
            }) 

我正在尝试从另一个模型创建一个 stock.picking。

但是,使用这种方法,我遇到了 move_lines 问题,它在 stock.picking 上与 stock.move 相关。

现在,它抛给我这个:

        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: Product Unit of Measure - product.uom] 

我知道有一些 stock.move 必填字段在我的 order_lines 字段中不存在。

所以,我的问题是,如何传递我的模型所需的例如 product.uom 或 date_expected?

在我的示例中,是否有一些与 picking_type_id 类似的方法。对于 One2many 字段?

请您参考如下方法:

您可以看看当您不在界面中填写这些字段时会发生什么(并查看它们默认得到的内容)。例如,在 stock.move 中,Odoo 从 product_id 字段中获取 product_uom(通过 onchange 方法)。 date_expected 默认填写当前日期。所以:

@api.multi 
def create_printy(self): 
    copy_record = self.env['stock.picking']  
    for record in self: 
        order_lines = [] 
        for rec in record.order_lines: 
            order_lines.append( 
            (0,0, 
            { 
                'product_id': rec.isbn.id, 
                'product_uom': rec.isbn.uom_id.id, 
                'date_expected': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT), 
                'product_qty': rec.qty, 
                } 
            )) 
        sp_types = self.env['stock.picking.type'].search([ 
        ('code', '=', 'outgoing') 
        ]) 
        if len(sp_types) > 0: 
            copy_record.create({ 
                'origin': record.name, 
                'picking_type_id': sp_types[0].id, 
                'move_lines': order_lines,  
                'move_type': 'direct', 
                'priority': '1', 
                'company_id': record.company_id.id, 
            }) 

对于 date_expected,您必须在您的 .py 文件中(在您的类之外)导入以下内容:

import time 
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT