Skip to content

[16.0][FIX] product_variant_configurator: prevent newId wrapping of existing template on new variants#446

Open
JordiMForgeFlow wants to merge 1 commit into
OCA:16.0from
ForgeFlow:16.0-fix-variant-configurator-clears-template-company-dependent-fields
Open

[16.0][FIX] product_variant_configurator: prevent newId wrapping of existing template on new variants#446
JordiMForgeFlow wants to merge 1 commit into
OCA:16.0from
ForgeFlow:16.0-fix-variant-configurator-clears-template-company-dependent-fields

Conversation

@JordiMForgeFlow

Copy link
Copy Markdown
Contributor

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:

  1. Create a new template. The company dependent fields should be filled in based on the default company properties (you can use for example property_stock_production or property_stock_inventory).
  2. From the product variants menu, create a new variant and assign it to the previously created template. The moment you assign it (even before saving the new variant) you will see that the company dependent fields get cleared out.
  3. When saving the new variant, the cleared out values are written and the product ends up without the values set.

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:

  1. 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.
  2. 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.

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.

@OCA-git-bot OCA-git-bot added series:16.0 mod:product_variant_configurator Module product_variant_configurator labels May 21, 2026
…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.
@JordiMForgeFlow JordiMForgeFlow force-pushed the 16.0-fix-variant-configurator-clears-template-company-dependent-fields branch from 563aa6d to 6622f73 Compare May 21, 2026 07:05

@AaronHForgeFlow AaronHForgeFlow left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:product_variant_configurator Module product_variant_configurator series:16.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants