Skip to content

Effortlessly Extend Odoo 18 Form View: Your Expert Guide

Odoo 18 Form View Extension

Looking to extend Odoo 18 form view capabilities without breaking a sweat? This expert guide will walk you through the essential steps to customize your Odoo system the right way. We will focus on a practical example: adding a dedicated “Payments Overview” tab to your Sales Order form. This enhancement will provide an immediate and clear view of all payment transactions related to each order, improving efficiency and reducing the need to navigate to separate payment records. Learn the powerful technique of inheritance to extend Odoo 18 form view professionally and safely.

Odoo 18 form view extension is a critical skill for taking your Odoo customization to the next level. This ultimate guide will show you how to master this functionality by creating a custom module from scratch. We will walk you through a real-world example of this powerful technique: adding a new “Payments” tab to the Sales Order form. This practical application of Odoo 18 form view extension will allow you to see all related payment transactions directly on the order. You will learn the professional way to customize Odoo, which involves inheritance, rather than risky direct modifications to core code.

Getting Started: Planning Your Odoo 18 Form View Extension

Before we dive into coding, let’s clearly define our objective. Our goal with this Odoo 18 form view extension is to modify the standard Sales Order view to include a new tab that displays all associated payment transactions. This involves not just showing all transactions, but also creating a separate, more advanced field that filters for only the valid, confirmed payments. This ensures we don’t count transactions that failed or were canceled.

Activating Developer Mode: Your Key to Customization

First things first, you cannot perform an Odoo 18 form view extension without enabling developer mode. This mode unlocks a suite of powerful tools that give you deep insight into the Odoo framework.

How to activate developer mode:

  1. Navigate to the main Settings menu in your Odoo instance.
  2. Scroll down to the bottom of the page.
  3. Click on Activate the developer mode. You can also activate it by adding ?debug=1 to your URL after web.

Once activated, you will see a new “bug” icon in the top right corner of your screen. This menu is your gateway to the technical information needed for any view customization.

Why a Custom Module is Essential for Odoo 18 Form View Extension

It might seem tempting to directly edit the existing Sales Order form view. Odoo’s developer mode even gives you an “Edit Form View” button that allows this. However, this is a dangerous practice in a production environment.

Here’s why direct editing is a bad idea:

  • Update Woes: When you update your Odoo instance or the core “Sales” module, your direct changes will be completely wiped out.
  • Risk of Errors: A small mistake, like a misplaced tag, can break the entire view, making it inaccessible.
  • Lack of Portability: Your changes are stuck in that one database. You cannot easily move them to another system.

The professional and scalable approach to Odoo 18 form view extension is to create a custom module. This encapsulates your changes, allowing you to install, uninstall, and update them safely without touching the core Odoo code.

Building Your Module for Odoo 18 Form View Extension

Let’s start building our sale_payment module. This module will contain all the logic and view definitions needed for our Odoo 18 form view extension.

Step 1: Create the Module Directory and Essential Files

First, create a new folder for your module inside your custom addons path. Let’s name it sale_payment. Inside this folder, you need two fundamental files:

  1. __init__.py: This file tells Python that the directory is a package.
  2. __manifest__.py: This is the module’s descriptor file containing metadata.

Next, create two subdirectories: models and views. Your initial structure should look like this:

sale_payment/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ __manifest__.py
β”œβ”€β”€ models/
β”‚   └── __init__.py
└── views/

Step 2: Configure the Manifest File

Now, open __manifest__.py and add the necessary information. The 'depends': ['sale'] entry is crucial for a successful Odoo 18 form view extension, as it ensures the base view is loaded first.

Python

{
    'name': 'Sale Order Payment Details',
    'version': '18.0.1.0',
    'summary': 'Adds a payment transaction tab to the sales order form.',
    'author': 'Your Name',
    'website': 'https://www.yourwebsite.com',
    'category': 'Sales',
    'depends': [
        'sale'
    ],
    'data': [
        'views/sale_order_views.xml',
    ],
    'installable': True,
    'application': False,
}

Implementing the Odoo 18 Form View Extension with XML

With the module structure in place, we can now focus on the core task. The core of our Odoo 18 form view extension lies in XML inheritance, a powerful feature that lets us alter existing views without changing the original code.

Step 1: Find the External ID of the View to Inherit

To inherit a view, you need its unique identifier, the External ID. For the base sales order form, it is sale.view_order_form. You can find this by navigating to a sales order and using the “Edit Form View” option in the developer menu.

Step 2: Creating the XML for Your Odoo 18 Form View Extension

Inside your views folder, create a new file named sale_order_views.xml. This file will contain the XML code to add our new tab.

XML

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="view_order_form_inherit_payment" model="ir.ui.view">
        <field name="name">sale.order.form.inherit.payment</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <notebook position="inside">
                <page name="payment_transactions" string="Payments">
                    <field name="transaction_ids"/>
                </page>
            </notebook>
        </field>
    </record>
</odoo>

The inherit_id tag is what links our new view to the original, forming the basis of the Odoo 18 form view extension.

Adding Advanced Logic with Python

While the XML adds the tab, we still need Python to create our advanced field for confirmed payments. For more advanced logic, your Odoo 18 form view extension will require Python inheritance.

Step 1: Python Inheritance for Advanced Odoo 18 Form View Extension

In your models folder, create sale_order.py. This is where we will add our custom field using model inheritance.

Python

from odoo import fields, models

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    confirmed_transaction_ids = fields.One2many(
        'payment.transaction',
        compute='_compute_confirmed_transactions',
        string='Confirmed Payment Transactions'
    )

    def _compute_confirmed_transactions(self):
        for order in self:
            order.confirmed_transaction_ids = order.transaction_ids.filtered(
                lambda t: t.state == 'done'
            )

The _inherit = 'sale.order' attribute tells Odoo to add the following fields to the existing sale.order model rather than creating a new one. The compute parameter points to the method that calculates the field’s value.

Step 2: Update the View with the New Field

Finally, update sale_order_views.xml to display our new computed field.

XML

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="view_order_form_inherit_payment" model="ir.ui.view">
        <field name="name">sale.order.form.inherit.payment</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <notebook position="inside">
                <page name="payment_transactions" string="Payments">
                    <separator string="Confirmed Transactions"/>
                    <field name="confirmed_transaction_ids"/>
                    <separator string="All Transactions"/>
                    <field name="transaction_ids"/>
                </page>
            </notebook>
        </field>
    </record>
</odoo>

Final Steps: Installation and Verification

Your module is now complete!

  1. Restart your Odoo server.
  2. Navigate to the Apps menu and click Update Apps List.
  3. Search for your sale_payment module and click Activate.

Now, open any sales order. You will see your brand new “Payments” tab. You have successfully implemented your first Odoo 18 form view extension. Mastering this technique opens up a world of possibilities for tailoring Odoo to your exact business needs.

https://www.odoo.com/documentation/18.0/developer/tutorials.html


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

Leave a Reply

WP Twitter Auto Publish Powered By : XYZScripts.com