覆盖一个字段:odoo.exceptions.CacheMiss:('stock.picking.batch(31,).picking_batch_moves', None)

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

我想把所有的动作都归为一个组。选股选股.分批 工作正常,但当我想覆盖掉 批量ID选股:

  File "/opt/odoo/odoo12/odoo/api.py", line 1051, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('stock.picking.batch(31,).picking_batch_moves', None)

这是我的代码。

class StockMove(models.Model):
    _inherit = 'stock.move'
    pbm_id = fields.Many2one('stock.picking.batch.move', string='Batche moves')


class StockPickingBatchLine(models.Model):
    _name = 'stock.picking.batch.move'
    _description = 'Opération des mouvement des transfer'

    batch_id = fields.Many2one(
        'stock.picking.batch', string='Picking batch', required=True, ondelete='cascade')
    product_id = fields.Many2one(
        'product.product', string='Produit', readonly=True, required=True)
    product_uom_id = fields.Many2one(
        'uom.uom', string='Unité de mesure', readonly=True, required=True)
    product_uom_qty = fields.Float('A faire', default=0.0, digits=dp.get_precision('Product Unit of Measure'),
                                   readonly=True, )
    location_id = fields.Many2one(
        'stock.location', 'From', readonly=True, required=True)
    location_dest_id = fields.Many2one(
        'stock.location', 'To', readonly=True, required=True)
    move_lines = fields.One2many(
        'stock.move', 'pbm_id', string='Movement de stock') 

class StockPickingBatch(models.Model):
    _inherit = 'stock.picking.batch'

    picking_batch_moves = fields.One2many('stock.picking.batch.move', 'batch_id', string='Lignes des mouvements',
                                          compute='_compute_picking_get_batch_lines', readonly=False, store=True,
                                          )

    @api.depends('picking_ids', 'picking_ids.move_lines')
    def _compute_picking_get_batch_lines(self):
        batch_moves_obj = self.env['stock.picking.batch.move']
        linked = self.env['stock.picking.batch.move']
        ml_ids = self.env['stock.picking.batch.move.line']
        for batch in self:
            if isinstance(batch.id, models.NewId):
                continue
            req = """
            SELECT sp.batch_id
                ,product_id
                ,product_uom product_uom_id
                ,sm.location_id
                ,sm.location_dest_id
                ,sm.state
                ,sm.picking_type_id
                ,sum(product_uom_qty) product_uom_qty
                ,array_agg(DISTINCT sm.id) moves
            FROM stock_move sm
            JOIN stock_picking sp ON sp.id = sm.picking_id
            WHERE sp.batch_id IN (%s)
            GROUP BY sp.batch_id
                ,product_id
                ,product_uom
                ,sm.location_id
                ,sm.state
                ,sm.picking_type_id
                ,sm.location_dest_id"""
            self.env.cr.execute(req, (batch.id,))
            fetched_lines = self.env.cr.fetchall()
            batch_moves = batch_moves_obj.search([('batch_id', '=', batch.id)])
            linked = batch_moves_obj
            move_lines = []
            for line in fetched_lines:
                # search for existing line to update
                matched = batch_moves.filtered(lambda x: x.product_id.id == line[1] and
                                                         x.product_uom_id.id == line[2] and
                                                         x.location_id.id == line[3] and
                                                         x.location_dest_id.id == line[4] and
                                                         x.state == line[5]
                                               )
                line_data = {
                    'batch_id': batch.id,
                    'product_id': line[1],
                    'product_uom_id': line[2],
                    'location_id': line[3],
                    'location_dest_id': line[4],
                    'state': line[5],
                    'picking_type_id': line[6],
                    'product_uom_qty': line[7],
                    'move_lines': [(6, 0, line[8])],
                }
                move_lines.extend(line[8])
                if matched.exists():
                    matched.with_context(recompute=False).write(line_data)
                    linked += matched
                else:
                    linked += batch_moves_obj.with_context(
                        recompute=False).create(line_data) 

            batch.picking_batch_moves = linked or False
python-3.x odoo odoo-12
1个回答
0
投票

试试吧。

batch.write({'picking_batch_moves': linked or False})

应该可以了

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