Keep an Odoo Computed Field Value When the Customer Changes

Aditya Oryza Mahendra
6 min Read
4 views

Keep an Odoo Computed Field Value When the Customer Changes

A user fills in the delivery address of a quotation first, then corrects the customer. The address they typed is gone, replaced by the new customer's default address. The data they entered was valid, and Odoo threw it away.

This post covers the cause, two fixes that were not enough, and the one that worked across CRM, sales orders and invoices.

The symptom

On a lead, a quotation or an invoice, the user picks a delivery address and then changes the customer. If the chosen address still belongs to the new customer (the same company, a branch of the same group), they expect it to stay. Instead it resets to the default address of the new customer. Nothing is logged, and the user only notices when the wrong address reaches a document.

The root cause: a stored compute that ignores its own value

In recent Odoo versions this field is a stored computed field that the user can still edit:

partner_shipping_id = fields.Many2one( "res.partner", compute="_compute_partner_shipping_id", store=True, readonly=False, precompute=True, ) @api.depends("partner_id") def _compute_partner_shipping_id(self): for order in self: order.partner_shipping_id = ( order.partner_id.address_get(["delivery"])["delivery"] if order.partner_id else False )

readonly=False lets the user type a value, but the compute still runs every time a dependency changes, and it assigns the default without looking at the current value. The value typed by the user and the value computed by the rule are the same field, and the rule always wins.

First fix: make the field a plain field

Removing the compute stops the reset, and breaks the feature that was there on purpose: the address is no longer filled when the customer is picked. You trade a small annoyance for a missing default.

Second fix: only compute when the field is empty

A guard such as if not order.partner_shipping_id: keeps the typed value. It also keeps the wrong one: change the quotation from customer A to customer B, and the address of A stays on a document of B. Keeping the value is only right when the value is still valid.

The fix that worked: keep it if it is still valid

The rule has two parts: keep the current address when it fits the new customer, otherwise fall back to the default. "Fits" needs a definition. Here it means: the customer itself, or a delivery address under the customer, its parent or its commercial partner.

class ResPartner(models.Model): _inherit = "res.partner" def _is_delivery_address_of(self, partner): self.ensure_one() if not partner: return False owners = partner | partner.parent_id | partner.commercial_partner_id return self in partner or ( self.type == "delivery" and self.parent_id in owners ) def _keep_or_default_delivery(self, partner): """Keep self if it is still valid for partner, else the default address.""" if not partner: return self.browse() if self and self._is_delivery_address_of(partner): return self return self.browse(partner.address_get(["delivery"])["delivery"])

(The snippet above is simplified from the real implementation.)

The compute then becomes one line per model, and the same helper serves every model:

order.partner_shipping_id = order.partner_shipping_id._keep_or_default_delivery(order.partner_id)

The behavior is now:

  1. The user picks an address of customer A, then switches to a branch of A: the address stays.
  2. The user switches to an unrelated customer B: the address becomes B's default, because A's address is not valid for B.
  3. The user clears the customer: the address is cleared.

Four details that are easy to miss

Apply the rule everywhere the field is reset. The core compute is only one place. A module that overrides write or an onchange and calls address_get again will bring the bug back for its own model (a subscription or a rental, for example). Search the code for every assignment of the field.

A value passed in the context must follow the same rule. A quotation created from an opportunity gets the opportunity's address through a context default. Honor it for new records only, and run it through the same validity check, so an unrelated customer does not inherit an address that does not belong to it.

Unsaved records hold temporary copies. In a form that is not saved yet, many2many and many2one values are temporary records. Their id is not the database id, so a query like "the lines of these invoices" returns nothing. Use _origin (records._origin.ids) when you query from a compute or an onchange.

Add tracking=True if the change matters. Odoo tracks changes made by a stored compute too, so the automatic reset shows up in the chatter as a line "address: A → B". That is useful when someone asks why an address changed.

Why not just restrict the field with a domain?

A domain on the field only limits what the user can pick. It does not touch the value that is already stored, so a stale address can still sit in the field after the customer changes.

Trade-offs to know about

  • "Valid" is a business decision. Treating the parent's addresses as valid for a branch is convenient, and it will surprise someone whose branches have separate sites. Write the rule down.
  • The rule lives in several places. Without the shared helper, each model drifts.
  • More chatter lines. Tracking a field that changes automatically adds a line every time.
  • The core method can change between versions. Re-check the override when you upgrade.

How to test it

Test the compute directly, and the form behavior separately, because the form runs the compute through the onchange.

from odoo.tests import Form, TransactionCase class TestKeepAddress(TransactionCase): def test_kept_for_a_related_customer(self): order = self.env["sale.order"].create({ "partner_id": self.group.id, "partner_shipping_id": self.site.id, }) order.partner_id = self.branch self.assertEqual(order.partner_shipping_id, self.site) def test_form_resets_for_an_unrelated_customer(self): order = self.env["sale.order"].create({ "partner_id": self.group.id, "partner_shipping_id": self.site.id, }) with Form(order) as form: form.partner_id = self.other self.assertEqual(form.partner_shipping_id, self.other_default)

Then prove that the tests detect the bug: remove the fix and run them again. In my case, 15 of 26 tests failed without the fix and none failed with it. A test that passes both ways is not testing anything.

Takeaways

  1. A stored compute with readonly=False overwrites the user's value on every dependency change. That is its normal behavior, not a bug in your code.
  2. Keeping the value is not enough; keep it only when it is still valid.
  3. One helper, applied in every override of the field, keeps the models consistent.
  4. Use _origin when you query from a compute or an onchange.
  5. Check that your test fails without the fix.

If a field in your project is filled automatically but still editable, check whether its compute respects what the user already typed.

A

Author: Aditya Oryza Mahendra

Software Engineer with expertise in backend systems, Odoo ERP development (v11–19), and Web3/dApp development. Experienced in building scalable applications using Node.js, Express.js, React, and Next.js, alongside deep customization of Odoo ERP across logistics, finance, CRM, and manufacturing domains. Currently advancing into Web3 with hands-on experience building and deploying smart contracts and decentralized applications using Solidity, Hardhat, and Ethers.js. Strong background in SQL/NoSQL databases, Docker, and VPS/Linux server administration. Contributed to the automation of 500+ daily shipments at a national logistics company.

Connect on LinkedIn