How to Add a Field to an Existing Model in Odoo 18 by Developing a Custom Module

Customizing an existing model by adding fields is a common task when implementing Odoo to meet specific business needs. This guide will walk you through creating a custom module in Odoo 18 that adds a new field to an existing model. By doing this, you can extend Odoo’s default functionality and tailor it to your requirements without modifying core files, ensuring compatibility with future updates.

Why Use a Custom Module?

Using a custom module to add fields to existing models has several advantages:

  1. Modularity: Keeps customizations separate from core code, which makes maintenance easier.
  2. Upgrade-Friendly: Protects your customizations when updating Odoo.
  3. Reusability: Allows you to package and reuse your module across different Odoo instances.

Prerequisites

Before we get started, make sure you have:

  • Odoo 18 installed: Ensure you have Odoo 18 installed and running.
  • Access to Developer Mode: It’s easier to develop custom modules with Developer Mode enabled.
  • Basic Python and XML Knowledge: Familiarity with Python and XML will be helpful.
  • Admin Access: Ensure you have admin privileges for file access on your server.

Step 1: Plan Your Custom Field

Let’s say you want to add a "Preferred Delivery Date" field to the Sales Order model (sale.order). This field will store the customer’s requested delivery date for the order.

Step 2: Create the Module Structure

  1. Navigate to Your Odoo Addons Directory: Locate the directory where Odoo modules are stored (usually /odoo/addons).
  2. Create a New Folder for Your Module: In the addons directory, create a new folder for your custom module. Name it something relevant, like sales_order_customization.
  3. Module Structure: Inside your new folder, create the following basic structure:
    sales_order_customization/
    ├── __init__.py
    ├── __manifest__.py
    └── models/
        ├── __init__.py
        └── sale_order_extension.py
    

Step 3: Define the Manifest File

The __manifest__.py file is the configuration file that Odoo reads to understand the module’s metadata. Open this file and add the following code:

{
    'name': 'Sales Order Customization',
    'version': '1.0',
    'category': 'Sales',
    'summary': 'Adds a preferred delivery date field to sales orders.',
    'description': 'This module adds a new field to the Sales Order model to store the preferred delivery date for orders.',
    'author': 'Your Name',
    'depends': ['sale'],  # Specify dependencies, here we depend on the Sales module
    'data': [
        'views/sale_order_view.xml',  # XML file for views
    ],
    'installable': True,
    'application': False,
}

Step 4: Add a New Field to the Model

To add a new field, create the sale_order_extension.py file inside the models folder. Here’s the Python code to add the Preferred Delivery Date field to the sale.order model.

# sales_order_customization/models/sale_order_extension.py

from odoo import models, fields

class SaleOrder(models.Model):
    _inherit = 'sale.order'  # Inheriting the Sale Order model

    preferred_delivery_date = fields.Date(string='Preferred Delivery Date')

Explanation:

  • _inherit = 'sale.order': This tells Odoo that you’re extending the existing sale.order model.
  • preferred_delivery_date: Defines a new date field for storing the preferred delivery date.

Step 5: Initialize the Models in __init__.py

Ensure Odoo recognizes your custom model by importing it in the __init__.py file inside the models folder:

# sales_order_customization/models/__init__.py

from . import sale_order_extension

Similarly, in the main __init__.py file at the root of your module directory, import the models folder:

# sales_order_customization/__init__.py

from . import models

Step 6: Define the View for the New Field

To make the new field visible in the Sales Order form, create an XML file named sale_order_view.xml inside your module directory. Define the field's placement in the Sales Order form view.

<!-- sales_order_customization/views/sale_order_view.xml -->

<odoo>
    <record id="view_order_form_inherit" model="ir.ui.view">
        <field name="name">sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//sheet/group" position="inside">
                <group>
                    <field name="preferred_delivery_date"/>
                </group>
            </xpath>
        </field>
    </record>
</odoo>

Explanation:

  • inherit_id: Refers to the existing view (sale.view_order_form) that you want to extend.
  • xpath: Specifies where the new field should be inserted. In this case, we place it inside a group element within the sheet layout.
  • <field name="preferred_delivery_date"/>: Adds the new field to the form.

Step 7: Install Your Module

Now that you’ve created your module, it’s time to install it in Odoo.

  1. Restart Odoo: Restart the Odoo service to load the new module.
  2. Activate Developer Mode: Go to Settings > Activate the Developer Mode if it isn’t already enabled.
  3. Update the Apps List: In Settings, click on Apps, then Update Apps List. This refreshes the module list and includes your custom module.
  4. Install the Module: Search for your module by name (e.g., “Sales Order Customization”) and click Install.

Step 8: Test the New Field

  1. Go to the Sales app and open any Sales Order.
  2. You should see the Preferred Delivery Date field in the form. Test it by entering a date and saving the order.

Optional: Add Security and Access Rights

If your new field contains sensitive information or if you want to restrict who can edit it, consider adding security rules.

  1. Create a Security Directory: Add a security folder to your module.
  2. Add a Security File: Create a file like ir.model.access.csv in the security folder to define access rights.
  3. Define Access Rules: Specify who can read, write, create, or delete records with this field.

Conclusion

With these steps, you’ve successfully created a custom module that extends an existing model in Odoo 18 by adding a new field. This process allows you to enhance Odoo’s functionality to meet your business’s unique requirements while keeping your changes modular and upgrade-friendly.

Creating custom modules may seem complex initially, but it gives you full control over Odoo’s capabilities and allows you to create a tailored ERP experience that aligns with your business processes.

How to Write a New Module for Odoo from the Front End in Odoo Community Edition
Creating new App in Odoo Community Edition from Odoo Web interface