Fix warning in stock_move_common_dest

In this module: https://github.com/OCA/stock-logistics-warehouse/pull/808
A computed field uses "common_dest_move_ids" as dependency:

    @api.depends(
        "picking_type_id.display_completion_info",
        "move_lines.common_dest_move_ids.state",
    )
    def _compute_completion_info(self):

Which has the following effect when the ORM triggers changes:

odoo.osv.expression: Non-stored field stock.move.common_dest_move_ids cannot be searched.

Implement a search method to prevent this.
This commit is contained in:
Guewen Baconnier
2020-04-27 14:03:01 +02:00
committed by Sébastien BEAU
parent efb5d1d006
commit b737eb6868
2 changed files with 43 additions and 7 deletions

View File

@@ -10,17 +10,12 @@ class StockMove(models.Model):
common_dest_move_ids = fields.Many2many(
"stock.move",
compute="_compute_common_dest_move_ids",
search="_search_compute_dest_move_ids",
help="All the stock moves having a chained destination move sharing the"
" same picking as the actual move's destination move",
)
@api.depends(
"move_dest_ids",
"move_dest_ids.picking_id",
"move_dest_ids.picking_id.move_lines",
"move_dest_ids.picking_id.move_lines.move_orig_ids",
)
def _compute_common_dest_move_ids(self):
def _common_dest_move_query(self):
sql = """SELECT smmr.move_orig_id move_id
, array_agg(smmr2.move_orig_id) common_move_dest_ids
FROM stock_move_move_rel smmr
@@ -36,6 +31,16 @@ class StockMove(models.Model):
AND smmr.move_orig_id IN %s
GROUP BY smmr.move_orig_id;
"""
return sql
@api.depends(
"move_dest_ids",
"move_dest_ids.picking_id",
"move_dest_ids.picking_id.move_lines",
"move_dest_ids.picking_id.move_lines.move_orig_ids",
)
def _compute_common_dest_move_ids(self):
sql = self._common_dest_move_query()
self.env.cr.execute(sql, (tuple(self.ids),))
res = {
row.get("move_id"): row.get("common_move_dest_ids")
@@ -47,3 +52,16 @@ class StockMove(models.Model):
move.common_dest_move_ids = [(6, 0, common_move_ids)]
else:
move.common_dest_move_ids = [(5, 0, 0)]
def _search_compute_dest_move_ids(self, operator, value):
moves = self.search([("id", operator, value)])
if not moves:
return [("id", "=", 0)]
sql = self._common_dest_move_query()
self.env.cr.execute(sql, (tuple(moves.ids),))
res = [
move_dest_id
for row in self.env.cr.dictfetchall()
for move_dest_id in row.get("common_move_dest_ids") or []
]
return [("id", "in", res)]

View File

@@ -154,3 +154,21 @@ class TestCommonMoveDest(SavepointCase):
self.assertEqual(pack_move_1b.common_dest_move_ids, pack_move_1a)
self.assertFalse(ship_move_1a.common_dest_move_ids)
self.assertFalse(ship_move_1b.common_dest_move_ids)
self.assertEqual(
self.env["stock.move"].search(
[("common_dest_move_ids", "=", pick_move_1b.id)]
),
pick_move_1a,
)
self.assertEqual(
self.env["stock.move"].search(
[("common_dest_move_ids", "=", pick_move_1a.id)]
),
pick_move_1b,
)
self.assertEqual(
self.env["stock.move"].search(
[("common_dest_move_ids", "in", (pick_move_1a | pick_move_1b).ids)]
),
pick_move_1a | pick_move_1b,
)