How to Create a Custom Module in Odoo: A Step-by-Step Guide

Odoo is a powerful open-source ERP system that allows developers to create custom modules tailored to specific business needs. In this guide, we’ll walk through the process of creating a simple Odoo module step by step. Whether you’re new to Odoo or looking for a refresher, this blog is for you!

odoo module
1. Setting Up the Environment:

Before we start, ensure you have:

  • Odoo installed (we’ll use Odoo 18 as an example).
  • A code editor (e.g., VS Code or PyCharm).
  • Basic knowledge of Python and XML.
2. Understanding the Odoo Module Structure:

Every module in Odoo follows a standard structure:

  • my_module/
  • ├── init.py
  • ├── manifest.py
  • ├── models/
  • │ ├── init.py
  • │ └── my_model.py
  • ├── views/
  • │ └── my_model_views.xml

Each file serves a purpose:

  • __init__.py: Initializes Python files.
  • __manifest__.py: Contains module metadata.
  • models/: Holds Python files for database models.
  • views/: Defines how data is displayed.
3. Step-by-Step Guide to Creating a Module:
Step 1: Create the Module Folder:
  1. Navigate to the addons directory in your Odoo installation.
  2. Create a folder named my_module.
Step 2: Add the Manifest File:

Inside my_module, create a file named __manifest__.py:

{
    'name': 'My Custom Module',
    'version': '1.0',
    'summary': 'A simple module for demonstration',
    'category': 'Custom',
    'author': 'Your Name',
    'depends': ['base'],
    'data': [
        'views/my_model_views.xml',
    ],
    'installable': True,
    'application': True,
}
Step 3: Create the Models Directory:
  1. Inside my_module, create a folder named models.
  2. Add an __init__.py file with:
from . import my_model

3. Create a file named my_model.py

from odoo import models, fields

class MyModel(models.Model):
    _name = 'my.model'
    _description = 'My Custom Model'

    name = fields.Char(string='Name', required=True)
    description = fields.Text(string='Description')
Step 4: Add Views:
  1. Create a folder named views in my_module.
  2. Add a file named my_model_views.xml:
<odoo>
    <record id="view_my_model_tree" model="ir.ui.view">
        <field name="name">my.model.tree</field>
        <field name="model">my.model</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                <field name="description"/>
            </tree>
        </field>
    </record>

    <record id="view_my_model_form" model="ir.ui.view">
        <field name="name">my.model.form</field>
        <field name="model">my.model</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="name"/>
                        <field name="description"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>
</odoo>
Step 5: Update __init__.py

Ensure the __init__.py in your module’s root folder includes:

from . import models

4 . Install the Module

  1. Restart the Odoo server:
./odoo-bin -u my_module

2. Go to Apps in the Odoo UI and search for My Custom Module.

3. Click Install.

5. Test Your Module
  • Navigate to the menu where your module is available.
  • Create new records to ensure the module works as expected.
6. Tips for Enhancing Your Module
  • Use @api.depends for computed fields.
  • Use @api.constrains to add validation logic.
  • Add menu items in views/my_model_views.xml to improve navigation.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *