Search code examples
pythonormodooodoo-13

Odoo - can write method cause memory leak when using 5 and 3 commands?


I’m wondering if incorrectly used write method can lead to a “memory leak”.

Let’s suppose that I have some Sale Order that I need to recompute. So I would remove all previous order lines and then in a loop I would create new lines like this:

sale_order.write({'order_line':[(5, 0, 0)]}) # Remove all previous products

vals = []
for product in new_products:
    vals.append((0, 0, {'product_id': product.id, 'product_uom_qty': product.qty, …})) # Create new lines

sale_order.write({'order_line':vals}) # Add new lines

But in the documentation it says, that (5, 0, 0) is equivalent of calling (3, id, 0) for every id, and that this command does not deletes records from the database (only (2, id, 0) does).

So if I'm understanding correctly if I would run this code couple of times at the end I would be left with many lines that do not correspond to any order and just exist in the database?

If so - how should I do this without causing this leak? Do I need to use 2 instead of 3/5 commands?

Using Odoo13.


Solution

  • Just use unlink on the order lines field:

    sale_order.order_line.unlink()