在One2many字段中创建的数据也可用作其他模块上的数据

问题描述 投票:0回答:1

我希望每次在One2many字段上创建数据,同时我希望它在我的maintenance.equipment上保存为数据。我试图在插件中找到其他模块的解决方案,但我还没有找到答案。

情况是,在验证我的产品中的货件之前,我需要在其上输入序列号。我同时为该产品创建的每个序列号都用作我的设备名称。

this is the picture for the scenario

而那个序列号是sample1010,我需要它在模块维护设备中成为我的设备名称。我希望它能在我的设备上显示。

my equipment module

我所教的只是我需要做的就是创建像这样的Many2one和One2many字段

class StockPackOperation(models.Model):
    _inherit = 'stock.pack.operation'

    lines_ids = fields.One2many('maintenance.equipment', 'lines_id')
    sample = fields.Char(string="Sample")

class MaintenanceEquipment(models.Model):
    _inherit = 'maintenance.equipment'

    lines_id = fields.Many2one('stock.pack.operation')

但没有发生。请提供任何帮助或建议或建议。我需要这样做。感谢建议大师。我是odoo的新手。

python-2.7 module odoo odoo-10 one2many
1个回答
0
投票

这可以通过继承stock.pack.operation.lot类来实现,因为输入的序列号存储在此类中,包含lot_name(如果是传入的货件)和lot_id(如果是传出货物)。

您不需要关心出货,因为在出​​货货物中我们选择已有的序列号。 class StockPackOperationLot(models.Model):_ inherit ='stock.pack.operation.lot'

@api.model
def create(self, vals):
    res = super(StockPackOperationLot, self).create(vals)
    if vals.get('lot_name'):
        self.env['maintenance.equipment'].create({'name': vals.get('lot_name')})
    return res

@api.multi
def write(self, vals):
    if vals.get('lot_name'):
        lot_name = self.lot_name
        equipment_id = self.env['maintenance.equipment'].search([('name', '=', lot_name)])
        res = super(StockPackOperationLot, self).write(vals)
        equipment_id.name = vals.get('lot_name')
        return res
    else:
        return super(StockPackOperationLot, self).write(vals)

要使此功能正常工作,您需要确保设备名称是唯一的,否则您需要在每个stock.pack.operation.lot记录中存储相关设备ID,以便在用户编辑序列号时,设备也会更新,当设备名称没有唯一约束时。

希望这可以帮助你......

© www.soinside.com 2019 - 2024. All rights reserved.