How to Create a Module in Odoo17?

Abid Patel
28-Aug-2024 Updated : 30-Aug-2024

Learn step-by-step how to create a custom module in Odoo 17. This guide covers module structure, adding features, and best practices for Odoo development.

How to Create a Module in Odoo17?

Creating a custom module in Odoo is a crucial skill for developers aiming to extend the functionality of Odoo to suit specific business needs. This guide will take you through the entire process step-by-step, from setting up the necessary folders to testing your module. By the end of this tutorial, you'll have a working custom module in Odoo that you can further enhance as needed.

Step-by-Step create module Guide:

1. Create a Folder in the Custom Addons Directory                           
2. Create Required Files - __init__.py and __manifest__.py                           
3. Defining a New Model                           
4. Creating Views                           
5. Adding Security Rules                           
6. Create a Static Folder for Assets                           
7. Testing Your Module

1. Create a Folder in the Custom Addons Directory

The first step in creating a custom module in Odoo is to set up a directory structure for your module. Odoo organizes modules in directories, and each module must have its own directory within the 'addons' or 'custom addons' folder. This folder will house all the files related to your module.

Step 1. Navigate to your Odoo installation directory:

This is where the 'addons' folder is located.

Step 2. Create a new folder inside the 'custom addons' directory:

Name this folder according to your module. For example, if your module is called "Inventory Extension," you could name the folder 'inventory_extension'.                        
' path:- /odoo17/custom_addons/inventory_extension '

2:- Create Required Files -' __init__.py ' and ' __manifest__.py '.

Every Odoo module requires at least two essential files:' __init__.py ' and ' __manifest__.py '.

Create ' __init__.py '

The ' __init__.py ' file initializes the module by defining which Python files should be loaded when the module is executed.

Step 1:- Inside your module's folder, create the ' __init__.py ' file:

' __init__.py ' path:- /odoo/custom_addons/inventory_extension/__init__.py

Step 2:- Open the file and add the following code:
__init__.py
from . import models

This line tells Odoo to load the ' models ' directory, where you'll define your business logic.

Create ' __manifest__.py '

The ' __manifest__.py ' file contains the metadata about your module, such as its name, version, author, and dependencies.

Step 1:- Create the ' __manifest__.py ' file in the module's folder:

' __manifest__.py ' path:- /odoo/custom_addons/inventory_extension/__manifest__.py

Step 2:- Open the file and add the following code:
__manifest__.py
{
   'name': 'Inventory Extension',
   'version': '1.0',
   'summary': 'Custom module to extend inventory features',
   'author': 'Your Name',
   'category': 'Inventory',
   'depends': ['base', 'stock'],
   'data': [
             'views/inventory_extension_views.xml',
             'security/ir.model.access.csv',
            ],
   'installable': True,
   'application': False,
}                                                            

This manifest file declares the module's name, version, dependencies, and the data files it will load.

3. Defining a New Model

Models in Odoo represent database tables and are used to define business logic. In your module, you'll need to create a Python file where your model(s) will be defined.

Step 1. Create a ' models ' directory inside your module folder:

' path:- /odoo17/custom_addons/inventory_extension/models/inventory_extension.py '

Step 3. Open the file and define your model:
inventory_extension.py
from odoo import models, fields
     class InventoryExtension(models.Model):
            _name = 'inventory.extension'
            _description = 'Custom Inventory Extension'
            name = fields.Char(string='Item Name', required=True)
            quantity = fields.Integer(string='Quantity')
            description = fields.Text(string='Description')                        

This example defines a basic model with three fields: 'name', 'quantity', and 'description'.

4. Creating Views

Views in Odoo are used to define the user interface for your module. You'll typically create form views, list views, and menu items.

Step 1. Create a views directory inside your module folder:

' path:- /odoo17/custom_addons/inventory_extension/view '

Step 2. Inside the views directory, create an XML file for your views. For example,             
' inventory_extension_views.xml ':

' path:- /odoo17/custom_addons/inventory_extension/views/inventory_extension_views.xml '

Step 3. Open the XML file and define your views:
inventory_extension_views.xml
<odoo>
    <record id="view_inventory_extension_form" model="ir.ui.view">
            <field name="name">inventory.extension.form</field>
            <field name="model">inventory.extension</field>
            <field name="arch" type="xml">
                <form string="Inventory Extension">
                    <group>
                        <field name="name" />
                        <field name="quantity" />
                        <field name="description" />
                   </group>
              </form>
         </field>
     </record>
     <record id="view_inventory_extension_tree" model="ir.ui.view">
          <field name="name">inventory.extension.tree</field>
          <field name="model">inventory.extension</field>
          <field name="arch" type="xml">
                <tree string="Inventory Extension">
                   <field name="name" />
                   <field name="quantity" />
                </tree>
        </field>
    </record>
    <menuitem id="menu_inventory_extension" name="Inventory Extension" parent="stock.menu_stock_root" action="inventory_extension_action" />
</odoo>
                                              

This XML defines a form view and a list view for the ' inventory.extension ' model and adds a menu item under the Inventory section.

5. Adding Security Rules

Security rules in Odoo control access to your models. You'll need to define who can view, create, and modify records in your custom module.

Step 1. Create a 'security' directory inside your module folder:

path:- /odoo17/custom_addons/inventory_extension/security '

Step 2. Inside the 'security' directory, create an access control file. For example,          
ir.model.access.csv:

path:- /odoo17/custom_addons/inventory_extension/security/ir.model.access.csv '

Step 3. Open the file and define your access control rules:
inventory_extension_views.xml
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_inventory_extension,access.inventory.extension,model_inventory_extension,,1,1,1,1   

This line grants all users full access to the inventory.extension model. You can customize these rules based on user roles and permissions.

6. Create a Static Folder for Assets

If your module includes static files like images, JavaScript, or CSS, you should place them in a 'static' directory within your module.

Step 1. Create a 'static' directory inside your module folder:

path:- /odoo17/custom_addons/inventory_extension/static

Step 2. Inside the 'static' directory, create subfolders for your assets. For example:

path:- /odoo17/custom_addons/inventory_extension/static/description        
path:- /odoo17/custom_addons/inventory_extension/static/src

Step 2. Add your static files to the appropriate folders

For example, you could add a 'description' image or 'src' files like CSS and JavaScript.

7. Testing Your Module

Once you've created your module, it's time to test it. Here's how you can do it:

Restart the Odoo service to load your new module:

' sudo service odoo restart '

Log in to your Odoo instance, navigate to the Apps menu, and search for your module by its name (e.g., "Inventory Extension").

Install the module. Odoo will automatically recognize it and allow you to install it from the Apps dashboard.

Test the module by navigating to the menu item you created and interacting with your model via the views you defined. Make sure to check all functionalities, including data entry, form views, list views, and any custom logic you've added.

Conclusion

Creating a custom module in Odoo 17 is a rewarding process that allows you to extend the platform's capabilities to meet specific business needs. By following these steps—setting up your module structure, defining models and views, adding security rules, and testing—you can create a fully functional Odoo module from scratch. With the basics covered, you can continue to explore more advanced features such as workflows, automation, and reporting, making your module even more powerful.

Make a Comment

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