Fix Memory Limit Error in Odoo Module Installation

Why Do Memory Limit Errors Occur?

by Dharmesh Patel

Memory limit errors during the installation of Odoo modules can be a frustrating hurdle for developers. Generally, this type of error occurs when an app/module has a Compute+Store field in a table that has a large number of records, so during module installation, it creates fields and computes field values for table records, consuming all memory and triggering a memory limit error.

Imagine working on an Odoo module, only to encounter a memory limit error during installation. This setback not only disrupts your workflow but also delays project timelines and compromises productivity. Without a solution, you're left frustrated and unable to proceed with your development tasks.

Fear not! Odoo provides a powerful feature called "pre_init_hook" to mitigate this issue. The pre_init_hook allows developers to execute custom functions before module installation, enabling them to perform tasks such as creating Compute+Store fields without triggering memory limit errors.

Overview of 'pre_init_hook'

In Odoo, a pre_init_hook is a special function that you can define in your module to be executed before the module is installed (initialization). This function can be used for various preparatory tasks such as checking system requirements, modifying the database schema without going through the ORM (Object Relational Mapping), or setting up data in a way that's needed for your module to function correctly from the start. It's a powerful feature that allows developers to ensure the right conditions and configurations are in place before the module installation begins.

We can create a 'pre_init_hook' to create fields (Compute+Store) by querying the database directly to avoid running the computations for records during module installation.

Steps to Use pre_init_hook in Odoo

Here’s how you can define and use a pre_init_hook in your Odoo module:

1: Define the Hook Function

In any Python file within your module (commonly __init__.py or a separate file), define a function that you want to run before the module installation. This function should accept a single argument: the database cursor (cr).

pre_init_hook-define the hook function

2: Declare the Hook in __manifest__.py

In your module's __manifest__.py file, add a key named pre_init_hook and set its value to the string name of your function.

pre_init_hook-declare the hook in __manifest__.py

3: Implement and Test

With the pre_init_hook defined, Odoo will automatically call this function before starting the installation of your module. It’s important to thoroughly test your function to ensure it does not inadvertently prevent your module from being installed under normal circumstances.

Using pre_init_hook wisely can greatly enhance the reliability and consistency of your module installations, ensuring that your module operates in a well-defined environment right from the start.

Customise Odoo FormViews using JavaScript
Getting help from JS in an Odoo ERP FormView