From 46f360f14d528fe2065b24f1c2098b11db7507ae Mon Sep 17 00:00:00 2001 From: CarmenMiranda Date: Tue, 15 Jul 2025 00:08:12 +0000 Subject: [PATCH] [FIX] purchase: Pre-Creation of `purchase_method` Adding a pre-init hook to pre-create the field `purchase_method` and set a default value with a sql query to avoid the computation on python. This commit includes changes for the ircodoo project's issue 3675. --- addons/purchase/__init__.py | 1 + addons/purchase/__manifest__.py | 1 + addons/purchase/hooks.py | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 addons/purchase/hooks.py diff --git a/addons/purchase/__init__.py b/addons/purchase/__init__.py index f18a4646bbd02f..edb580d74d5073 100644 --- a/addons/purchase/__init__.py +++ b/addons/purchase/__init__.py @@ -5,3 +5,4 @@ from . import models from . import report from . import populate +from .hooks import pre_init_hook diff --git a/addons/purchase/__manifest__.py b/addons/purchase/__manifest__.py index 1c36c885126c36..a63803215571cc 100644 --- a/addons/purchase/__manifest__.py +++ b/addons/purchase/__manifest__.py @@ -34,6 +34,7 @@ 'demo': [ 'data/purchase_demo.xml', ], + "pre_init_hook": "pre_init_hook", 'installable': True, 'application': True, 'assets': { diff --git a/addons/purchase/hooks.py b/addons/purchase/hooks.py new file mode 100644 index 00000000000000..78317ed7eb49ca --- /dev/null +++ b/addons/purchase/hooks.py @@ -0,0 +1,20 @@ +import logging + +from odoo.tools import column_exists + +_logger = logging.getLogger(__name__) + + +def pre_init_hook(cr): + pre_create_purchase_method(cr) + + +def pre_create_purchase_method(cr): + """Create the purchase_method field in product.template if it does not exist (added in "purchase" dependency). + Also, set the default value to 'purchase' for existing products by query instead of computing it with Python. + """ + if not column_exists(cr, "product_template", "purchase_method"): + cr.execute("ALTER TABLE product_template ADD COLUMN purchase_method character varying;") + _logger.info("Column 'purchase_method' added to product_template table.") + cr.execute("UPDATE product_template AS pt SET purchase_method = 'purchase';") + _logger.info("Setting purchase_method to 'purchase' for %s products", cr.rowcount)