What is Override in Odoo?

What is Override in Odoo?

Basically, overrides allow you to override or change the default behavior of Odoo modules. This means you can customize the appearance, functionality, and workflow of Odoo without directly modifying the core source code. This is very important to maintain the integrity of updates and reduce the risk of problems arising when new versions of Odoo are released.

Override Type

There are two main types of overrides in Odoo:
  • View Override: Allows you to customize how data is displayed in Odoo. Use this to:
    • Add or remove fields to forms, list views, and reports (views).
    • Modify the design and layout of UI elements.
    • Implemented new widgets.
  • Business Logic Override: Allows you to change how Odoo models work. Use this to:
    • Added new methods and functions.
    • Modify existing methods and functions.
    • Implement new business rules and restrictions.

Key Benefits of Using Override

  • Customization: Customize Odoo precisely according to your specific workflow and business needs.
  • Extensibility: Add new functions and features beyond what Odoo provides by default.
  • Maintainability: Perform Odoo updates smoothly without losing your customizations.
  • Efficiency: Automate tasks and create solutions that increase employee productivity and operational efficiency.

Example of Override Implementation

1. Display Override

<?xml version="1.0" encoding="utf-8"?>

        <record id="sale.action_orders" model="ir.actions.act_window">
            <field name="name">Sales Orders</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">sale.order</field>
            <field name="view_mode">tree,kanban,form,calendar,pivot,graph,activity</field>
            <field name="search_view_id" ref="sale.sale_order_view_search_inherit_sale"/>
            <field name="context">{'default_z_custom_so':True}</field>
            <field name="domain">[('state', 'not in', ('draft', 'sent', 'cancel'))]</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    Create a new quotation, the first step of a new sale!
                    Once the quotation is confirmed, it becomes a sales order.<br/> You will be able to create an invoice and collect the payment.

This code modifies the default "action" behavior for Sales Orders in Odoo. These changes include the initial appearance of view modes, filtering, and adding a default context.

Detailed Explanation

  • <record id="sale.action_orders" model="ir.actions.act_window">: This code targets the action named "sale.action_orders" (the default action for accessing Sales Orders).
  • <field name="...">: Various fields here modify aspects of this action:
    • view_mode: Set the view mode (list, kanban, form, etc.) that will be enabled by default.
    • search_view_id: Sets the search view used.
    • context: Added a default context {'default_z_custom_so': True} that may affect other behavior in Odoo.
    • domain: Set a default filter to hide orders with 'draft', 'sent' and 'cancel' statuses.
    • help: Provides a custom help message for users when accessing this action.

2. Function Override

def action_confirm(self):
if not self.order_line:
raise ValidationError('Product is empty. Please check again.')
return super(SaleOrder, self).action_confirm()
This code adds validation to the "Sale Order" model in Odoo. This validation prevents confirmation (status change) of a Sales Order if there is not at least one product in it.

Detailed Explanation

  • def action_confirm(self):: Defines a function named "action_confirm". Most likely this function overrides the default action_confirm function in the "Sale Order" model.
  • if not self.order_line:: Checks whether the order_line field (list of product lines in the order) is empty. This field is usually a One2many or Many2many relationship.
  • raise ValidationError('Product is empty. Please check again.'): If order_line is empty, an error will be raised with the message "Product is empty. Please check again." the type is "ValidationError" which will stop the process.
  • return super(SaleOrder, self).action_confirm(): If validation is successful, this code will call the original (default) action_confirm function on the "SaleOrder" model continuing the confirmation process.
We are a community of Computer Technology enthusiasts, and we are very happy that you decided to join.