From ef9014332ae365b6404e86eeea9d2f55dcf4c702 Mon Sep 17 00:00:00 2001 From: Eric Antones Date: Thu, 2 Apr 2026 15:56:48 +0200 Subject: [PATCH] [IMP] rental_base_extension: add post_init_hook for existing records When the rental modules are installed on a database with existing sale orders, several fields remain NULL because defaults and computes only apply to new records. This causes errors when editing old quotations (the Order Type radio buttons appear blank, attrs evaluation fails). The post_init_hook sets the correct default values on existing records: - sale_order.type_id = normal_sale_type (xmlid via ORM, update via SQL) - sale_order_line.rental = False - sale_order_line.can_sell_rental = False --- rental_base_extension/__init__.py | 1 + rental_base_extension/__manifest__.py | 1 + rental_base_extension/hooks.py | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 rental_base_extension/hooks.py diff --git a/rental_base_extension/__init__.py b/rental_base_extension/__init__.py index 0650744f6..cc6b6354a 100644 --- a/rental_base_extension/__init__.py +++ b/rental_base_extension/__init__.py @@ -1 +1,2 @@ from . import models +from .hooks import post_init_hook diff --git a/rental_base_extension/__manifest__.py b/rental_base_extension/__manifest__.py index 2fc4f298a..03758cbcf 100644 --- a/rental_base_extension/__manifest__.py +++ b/rental_base_extension/__manifest__.py @@ -11,6 +11,7 @@ "author": "NuoBiT Solutions SL", "website": "https://github.com/nuobit/odoo-addons", "license": "AGPL-3", + "post_init_hook": "post_init_hook", "depends": [ "rental_base", ], diff --git a/rental_base_extension/hooks.py b/rental_base_extension/hooks.py new file mode 100644 index 000000000..08dd1d6cb --- /dev/null +++ b/rental_base_extension/hooks.py @@ -0,0 +1,26 @@ +# Copyright 2026 NuoBiT Solutions SL - Eric Antones +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) + +import logging + +from odoo import SUPERUSER_ID, api + +_logger = logging.getLogger(__name__) + + +def post_init_hook(cr, registry): + env = api.Environment(cr, SUPERUSER_ID, {}) + normal_type = env.ref("sale_order_type.normal_sale_type") + cr.execute( + "UPDATE sale_order SET type_id = %s WHERE type_id IS NULL", + [normal_type.id], + ) + _logger.info("Updated %d sale orders with default type_id", cr.rowcount) + cr.execute("UPDATE sale_order_line SET rental = False WHERE rental IS NULL") + _logger.info("Updated %d sale order lines with rental = False", cr.rowcount) + cr.execute( + "UPDATE sale_order_line SET can_sell_rental = False WHERE can_sell_rental IS NULL" + ) + _logger.info( + "Updated %d sale order lines with can_sell_rental = False", cr.rowcount + )