Skip to content

Commit

Permalink
stock_reception_screen: fix perf issue when move are created
Browse files Browse the repository at this point in the history
When moves are created, Odoo triggers the computation of
related/computed fields that could be impacted by this change.

To compute the quantities the previous code was triggering a SQL query on
'stock_move' without any clause to satisfy the 'product.stock_move_ids'
One2many field: "SELECT id FROM stock_move WHERE product_id IN (...);",
hammering PostgreSQL and increasing the time needed to create a move.

By removing the dependencies on these quantity fields (that are
depending on 'product.stock_move_ids' field), we avoid this huge query
on the DB, speeding up the time required to create a move.

This should not impact the users of the reception screen as these
quantities are only displayed for information when scanning a received
product.
  • Loading branch information
sebalix committed Mar 25, 2024
1 parent c114af4 commit a331c4a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions stock_reception_screen/models/stock_reception_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ class StockReceptionScreen(models.Model):
related="current_move_id.vendor_code"
)
current_move_product_qty_available = fields.Float(
related="current_move_id.product_id.qty_available"
compute="_compute_current_move_product_qty_fields"
)
current_move_product_outgoing_qty = fields.Float(
related="current_move_id.product_id.outgoing_qty"
compute="_compute_current_move_product_qty_fields"
)
current_move_product_last_lot_expiration_date = fields.Datetime(
compute="_compute_current_move_product_last_lot_expiration_date",
Expand Down Expand Up @@ -205,6 +205,20 @@ class StockReceptionScreen(models.Model):
warn_notification = fields.Char(default=False)
warn_notification_html = fields.Html(compute="_compute_warn_notification")

@api.depends(
# NOTE: do not depends on qty product fields on purpose,
# Odoo ORM triggers a global search on stock.moves when a move is created
"current_move_id.product_id"
)
def _compute_current_move_product_qty_fields(self):
for rec in self:
rec.current_move_product_qty_available = (
rec.current_move_id.product_id.qty_available
)
rec.current_move_product_outgoing_qty = (
rec.current_move_id.product_id.outgoing_qty
)

@api.depends("warn_notification")
def _compute_warn_notification(self):
warn_massage_html = """
Expand Down

0 comments on commit a331c4a

Please sign in to comment.