[16.0][FIX] product_variant_configurator: prevent newId wrapping of existing template on new variants#446
Open
JordiMForgeFlow wants to merge 1 commit into
Conversation
…g template on new variants When a new ``product.product`` is attached to an existing template through the variant configurator's form, Odoo's ``Many2one.convert_to_cache`` wraps the parent template id in ``NewId``: the standard "if all records are new, then so is the parent" rule for delegate (``_inherits``) Many2one fields. That fits Odoo's native flow (new variant auto-created alongside a new template) but breaks this module's flow (new variant attached to an *existing* template): the wrap then makes ``Field._compute_company_dependent`` reads on the parent return empty, because its result dict is keyed by real ids while the loop uses the record's ``NewId`` as the lookup key. The user-visible effect: inherited ``company_dependent`` fields on the variant form (``property_stock_production``, ``property_stock_inventory``, the various accounting properties, ...) go empty as soon as a template is selected, and the empty value is then sent back to the template via the ``_inherits`` write-back on save, clearing the template's per-company ``ir.property`` row. Patch ``fields.Many2one.convert_to_cache`` at module-import time (from ``hooks.py``) to short-circuit the NewId wrap when the input is a positive real id and the child record is new. All other call paths fall through to the standard behaviour, so the native "create both records together" flow is preserved.
563aa6d to
6622f73
Compare
AaronHForgeFlow
approved these changes
May 21, 2026
AaronHForgeFlow
left a comment
There was a problem hiding this comment.
Code review LGTM.
Nice catch!
JordiMForgeFlow
added a commit
to ForgeFlow/odoo
that referenced
this pull request
May 21, 2026
When creating a new record that gets assigned to an already existing parent, wrapping it in NewId causes company-dependent fields managed on the parent to not be correctly evaluated. Issue is discovered with product.template and product.product, using an extension module that allows assigning a new product variant to an existing template. The issue is affecting company-dependent fields that are managed at the product template level. It actually also affects other models that follow the _inherits structure. The reason why this happens is because: For m2o fields related to _inherits structure (delegated) Odoo wraps the value as a newId if we are creating a new record: https://github.com/odoo/odoo/blob/c415f984c8ba4e636801fce752eb6fd2c0b8082e/odoo/fields.py#L3011. Odoo basically assumes that, if you are creating a new variant in the Form, you are also creating a new template. When the template is assigned, the fields are then computed in https://github.com/odoo/odoo/blob/c415f984c8ba4e636801fce752eb6fd2c0b8082e/odoo/fields.py#L749 but the template is evaluated as a NewId so the results are False. More info: OCA/product-variant#446
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This issue was noticed because some products were missing the values of property_stock_production and property_stock_inventory when there are default company properties that set their values.
The issue is reproduced like:
After investigating, the issue is affecting company-dependent fields that are managed at the product template level. It actually also affects other models that follow the _inherits structure. The reason why this happens is because:
A patch on the convert_to_cache method solves the issue and seems to be the right place to fix. Nonetheless, I am interested in other opinions on this.