Post load hook in Odoo 18
Enterprise
Enterprise
Introduction
In this slide, we’ll discuss on the post load hook in Odoo 18.
post_load is a hook in Odoo executed after modules are
loaded but before the application fully starts. It is ideal for
server-wide changes and runtime behavior customization. This
is often used for monkey patching and initializing
configurations. Key benefit of this is that it enables global
changes without affecting individual module registries.
Enterprise
The post_load hook is commonly used for monkey patching, a
programming technique where you dynamically modify or extend
existing functionality at runtime. In Odoo, this is particularly useful
for overriding or enhancing core functionalities globally without
directly modifying the source code. Let us consider the example of
overriding the email sending behavior in Odoo. We can log all
outgoing email messages in a custom format before they are sent,
without altering Odoo’s source code.
Enterprise
In the __init__.py file of custom module, we can add the
following custom code.
from odoo.addons.mail.models.mail_mail import Mail
def custom_email_send():
original_send = Mail.send # Save the reference to the
original method
def new_send(self, auto_commit=False,
raise_exception=False):
# Log the email details
for mail in self:
_logger.info(f"Sending email with subject:
{mail.subject} and recipients: {mail.email_to}")
# Call the original send method to maintain
functionality
return original_send(self, auto_commit=auto_commit,
raise_exception=raise_exception)
Mail.send = new_send
Enterprise
After that we can add this in the __manifest__.py file.
In
{
'name': 'Custom Email Logger',
'version': '1.0',
'summary': 'Log email details before
sending',
'post_load': 'custom_email_send',
'installable': True,
'application': False,
}
Enterprise
In this example, the Mail.send method from Odoo’s mail.mail model
is monkey patched. Before sending an email, details like the subject
and recipients are logged using _logger. The original send method
is invoked after logging, ensuring the normal email functionality
remains intact. Using post_load ensures that the monkey patch is
applied only once, during the module’s initialization. This avoids
issues where the method might be redefined multiple times,
especially in cases where the module might exist in multiple
locations in the addons path.
For More Info.
Check our company website for related
blogs and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

What is the Post load hook in Odoo 18

  • 1.
    Post load hookin Odoo 18 Enterprise
  • 2.
    Enterprise Introduction In this slide,we’ll discuss on the post load hook in Odoo 18. post_load is a hook in Odoo executed after modules are loaded but before the application fully starts. It is ideal for server-wide changes and runtime behavior customization. This is often used for monkey patching and initializing configurations. Key benefit of this is that it enables global changes without affecting individual module registries.
  • 3.
    Enterprise The post_load hookis commonly used for monkey patching, a programming technique where you dynamically modify or extend existing functionality at runtime. In Odoo, this is particularly useful for overriding or enhancing core functionalities globally without directly modifying the source code. Let us consider the example of overriding the email sending behavior in Odoo. We can log all outgoing email messages in a custom format before they are sent, without altering Odoo’s source code.
  • 4.
    Enterprise In the __init__.pyfile of custom module, we can add the following custom code. from odoo.addons.mail.models.mail_mail import Mail def custom_email_send(): original_send = Mail.send # Save the reference to the original method def new_send(self, auto_commit=False, raise_exception=False): # Log the email details for mail in self: _logger.info(f"Sending email with subject: {mail.subject} and recipients: {mail.email_to}") # Call the original send method to maintain functionality return original_send(self, auto_commit=auto_commit, raise_exception=raise_exception) Mail.send = new_send
  • 5.
    Enterprise After that wecan add this in the __manifest__.py file. In { 'name': 'Custom Email Logger', 'version': '1.0', 'summary': 'Log email details before sending', 'post_load': 'custom_email_send', 'installable': True, 'application': False, }
  • 6.
    Enterprise In this example,the Mail.send method from Odoo’s mail.mail model is monkey patched. Before sending an email, details like the subject and recipients are logged using _logger. The original send method is invoked after logging, ensuring the normal email functionality remains intact. Using post_load ensures that the monkey patch is applied only once, during the module’s initialization. This avoids issues where the method might be redefined multiple times, especially in cases where the module might exist in multiple locations in the addons path.
  • 7.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com