Skip to Content

Re: Warning about creating a product and reusing its variable

Ooh, thanks for that warning.

I would have said bug for the resulting recordset to have context values left over from its own needs...

Good luck getting that sorted, though, without proposing the fix yourself....

Cheers


Kind regards,
Richard deMeester
Development QA
WilldooIT website
Facebook
LinkedIn
PNORS website
Pacific Commerce website
WilldooIT website
Netway Networks website
WilldooIT is a member of the PNORS Technology Group.
This email and any files transmitted with it are confidential and are intended solely for the use of the individual or entity to which they are addressed. If you are not the intended recipient, you may not disclose or use the information in this email in any way. If you have received this email in error please notify the sender. Although reasonable precautions have been taken to ensure no viruses are present in this email, no responsibility is accepted by PNORS Technology Group Pty Ltd or its related entities for any loss or damage arising from the use of this email or attachments. Any views expressed in this email or file attachments are those of the individual sender only, unless expressly stated to be those of PNORS Technology Group Pty Ltd or any of its related entities.

From: Yann Papouin <notifications@odoo-community.org>
Sent: Saturday, 7 February 2026 2:37 AM
To: Contributors <contributors@odoo-community.org>
Subject: Warning about creating a product and reusing its variable
 
Hi everyone,

A small tip to avoid an headache :

If you create a new product
    item_a = self.env["product.product"].create({"name": "ItemA"})
And then copy it
    item_b = item_a.copy({"name": "ItemB"})

You will have the surprise to see that "item_b" is en empty recordset.

Why ? Simply because a context key is set by odoo (create_product_product=False) when creating a `product.product`
So to avoid this issue, simply re-browse your newly created record to have a clean context:
    item_a = self.env["product.product"].create({"name": "ItemA"})
    item_a = self.env["product.product"].browse(item_a.id)
    item_b = item_a.copy({"name": "ItemB"})

It is the second time in a week that I have an issue with context keys set inside a `create that stays in the context of the variable.

I don't know if it should be considered a bug or not but I believe that if a context key is set from create/copy/etc. to fix some recursion issue, the context should be cleaned after super() before returning the final value

--
Yann PAPOUIN, Ingénieur R&D | DEC

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe


by "Richard deMeester" <richard.demeester@willdooit.com> - 09:36 - 9 Feb 2026

Reference

  • Warning about creating a product and reusing its variable
    Hi everyone,

    A small tip to avoid an headache :

    If you create a new product
        item_a = self.env["product.product"].create({"name": "ItemA"})
    And then copy it
        item_b = item_a.copy({"name": "ItemB"})

    You will have the surprise to see that "item_b" is en empty recordset.

    Why ? Simply because a context key is set by odoo (create_product_product=False) when creating a `product.product`
    So to avoid this issue, simply re-browse your newly created record to have a clean context:
        item_a = self.env["product.product"].create({"name": "ItemA"})
        item_a = self.env["product.product"].browse(item_a.id)
        item_b = item_a.copy({"name": "ItemB"})

    It is the second time in a week that I have an issue with context keys set inside a `create that stays in the context of the variable.

    I don't know if it should be considered a bug or not but I believe that if a context key is set from create/copy/etc. to fix some recursion issue, the context should be cleaned after super() before returning the final value

    --
    Yann PAPOUIN, Ingénieur R&D | DEC


    by Yann Papouin - 04:35 - 6 Feb 2026