Odoo is a collection of open-source business apps that covers all the business needs of a company, such as CRM, ERP, inventory, accounting, project management, e-commerce, and many more.  The platform is easy to use, fully flexible, and highly cost-effective. 

The functioning of a business nowadays is on the harder side without dedicated functional tools and software solutions. The Odoo module can either add brand new business logic to an Odoo system or alter and extend existing business logic. Everything in Odoo starts and ends with modules, from setting generic accounting support to real-time visualization of the bus fleet. 

In this, we will focus on how to create a module. And how to design the views in this module. 

What are Odoo Modules? 

Odoo modules are vital building blocks that extend the capabilities of the Odoo ERP (Enterprise Resource Planning) system. The modules enable specific features or business functions, allowing organizations to customize and grow their Odoo implementation to meet their individual needs.

Some of the key points of the Odoo Modules are:-

  • Wide Range of Functionalities
  • Customization and Flexibility
  • Integration Capabilities
  • Community and Enterprise Versions
  • Development and Extension
  • Module Structure
  • Installation and Configuration

Steps to Create and Setup Odoo Module

Step1: Set Up a Development Environment: 

Setting up a development environment for Odoo involves several key steps:

1. Install Python: 

  • Compatibility: Ensure you have a Python version compatible with the Odoo version you plan to work with. Odoo usually supports recent Python versions, but it’s best to check the specific requirements for your Odoo version.
  • Installation: You can download and install Python from the official Python website. Follow the instructions for your operating system.

2. Set Up a Virtual Environment: 

  • Purpose: A virtual environment allows you to manage Python dependencies for different projects separately. It’s particularly useful for keeping your Odoo project’s dependencies isolated.
  • Creation: Use Python’s built-in ‘venv’ module to create a virtual environment. For example, ‘python -m venv odoo-env’.
  • Activation: Activate the virtual environment before proceeding. On Windows, use ‘odoo-env\Scripts\activate’, and on Unix or MacOS, use ‘source odoo-env/bin/activate’.

3. Install PostgreSQL: 

  • Requirement: Odoo uses PostgreSQL as its database backend. It’s necessary to have it installed and running.
  • Installation: You can download PostgreSQL from here. Follow the installation guide for your specific operating system.
  • Configuration: After installation, ensure that PostgreSQL is running and you can connect to it. You might need to create a new database user for Odoo.

4. Install Odoo: 

  • Download Source Code: Get the Odoo source code from Odoo’s GitHub repository or their official website. Choose the version that you plan to develop your module for.
  • Installation: Follow the instructions in Odoo’s documentation for installing from source. This typically involves installing dependencies and possibly compiling some components.

5. Configure Odoo: 

  • Configuration File: Modify the Odoo configuration file to specify database parameters, add-on path, and other important settings. This file is crucial for customizing the behavior of your Odoo instance.
  • Settings: Pay special attention to database connection settings, log file paths, and the location of add-on modules.

6. Run Odoo Server:

  • Starting the Server: With everything set up, you can start the Odoo server. This is usually done via the command line, for example, ‘./odoo-bin -c /path/to/your/config.conf’.
  • Accessing Odoo: Once the server is running, you can access Odoo through your web browser, usually at ‘localhost:8069’.

Step2: Start/ Stop the Odoo Server 

With Odoo’s client/server architecture, web browsers connect to the Odoo server via Remote Procedure Calls (RPCs).

The server side typically handles business logic and extensions, but the client can also provide additional capabilities (like interactive maps) to provide new data representation.

Simply type the following command into the shell to launch the server: odoo-bin, providing the complete path to the file if needed.

odoo-bin

Either terminating the associated OS process or pressing Ctrl-C twice from the terminal will end the server.

Step3: Build an Odoo module 

Extensions for servers and clients are bundled as modules that can be inserted into databases on an optional basis.

Odoo modules can be used to extend or add new business logic to an Odoo system. For example, you can design a module that adds your nation’s accounting standards to Odoo’s generic accounting support and another module that adds support for real-time fleet visualization of buses.

Modules are, hence, the beginning and conclusion of everything in Odoo.

Composition of module 

There are multiple elements present in the Odoo module

Business Objects 

Contains Python Classes in the Python files 

Object Views 

Contains the business objects for UI

Data Files 

XML or CSV files declaring the model metadata:

Demonstration data

Views or reports 

Configuration data( modules parametrization, security rules) 

and more

Step4. Specify Module Structure 

Within a module directory, every module is a directory. You can specify module directories by using the –addons-path option.

A manifest is used to declare an Odoo module.

In addition, a module is a Python package that includes a __init__.py file that contains import guidelines for different Python files included in the module.

For example, if the module just has one mymodule.py file, then __init__.py might include:

from . import mymodule

Odoo has a tool to assist in setting up a new module. To construct an empty module, use the subcommand scaffold in odoo-bin:

odoo-bin scaffold <module name> <where to put it>

The tool automatically generates a number of standard files for a module and establishes a subdirectory for your module. The majority of them just have XML or commented code. Throughout this tutorial, the majority of the files’ uses will be discussed.

Step 5: Setup Object- Relational Mapping (ORM)

One of the main parts of Odoo is the ORM layer. This layer offers security services and extensibility and eliminates the need to write the majority of SQL by hand.

Business objects are integrated into the automated persistent system via declaration as Python classes extending Mode1.

By configuring certain attributes at the model’s definition, models can be made more functional. The most crucial property is _name, which is necessary and establishes the model’s name within the Odoo system. An incomplete definition of a model is as follows:

from odoo import models
class MinimalModel(models.Model):
    _name = 'test.model'

Step 6: Model Field

What and where the model can store the fields are used to define that. The fields are defined as attributes on the model class. 

from odoo import models, fields
class LessMinimalModel(models.Model):
    _name = 'test.model2'
    name = fields.Char()

Step 7: Configure Common Attributes 

Its fields, like the model itself, can be configured by giving configuration attributes as parameters:

name = fields.Char(required=True)

Some attributes are available in all fields; the following are the most common:

string (unicode, field name by default)

The label of the field in the user interface (visible to users).

mandatory (bool, default: False)

If True, the field cannot be left empty; it must either have a default value or be assigned a value when a record is created.

help (unicode, default: ‘ ‘ )

Long-form gives users a help tooltip in the UI.

index (bool, default: false)

Odoo is asked to construct a database index on the column.

Step 8: Create Simple Fields 

Fields are classified into two types: “simple” fields, which are atomic values recorded directly in the model’s table, and “relational” fields, which link records (of the same or separate models).

Simple fields include Boolean, Date, and Char.

id (Id) A record’s unique identifier in its model.

date_of_creation (Datetime) The date the record was created.

create_uid (Many2one) Identifies the user who produced the record.

write_date(Datetime) The record’s most recent update date.

write_uid (Many2one) identifies the person who last edited the record.

Special Fields 

Odoo also requires a name field on all models by default for various display and search behaviors. Setting _rec_name overrides the field used for these purposes.

Step 9: Data Files 

The platform is a highly data-driven system. 

Although behavior can be customized with Python code, a large part of a module’s value lies in the data it creates when loaded.

Data files, XML files with <record> elements, are used to declare module data. A database record is created or updated by each <record> element.

<odoo>
        <record model="{model name}" id="{record identifier}">
            <field name="{a field name}">{a value}</field>
        </record>
</odoo>
  • For the record, the Odoo model is called model.
  • id is an external identifier that allows you to refer to a record without knowing its in-database identifier.
  • The name of the field in the model is assigned to <field> elements. Their body is the value of the field.

To load, the data files have to be declared in the manifest file; these can be declared in the ‘data’ list or in the ‘demo’ list. 

Odoo CTA

Step 10: Actions and Menus

Actions and menus are standard database records that are typically declared using data files. Actions can be triggered in three different ways:

  1. Selecting menu items (which are linked to specific actions)
  2. selecting buttons in views (if they are linked to actions)
  3. as contextual actions performed on an object

Because menus are relatively difficult to declare, there is a <menuitem> shortcut to more easily create an ir.ui.menu and tie it to the associated action.

<record model="ir.actions.act_window" id="action_list_ideas">
    <field name="name">Ideas</field>
    <field name="res_model">idea.idea</field>
    <field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_ideas" parent="menu_root" name="Ideas" sequence="10"
          action="action_list_ideas"/>

Step 11: Security and Access Rights

In Odoo, ensuring the security of your module and managing access rights are critical steps in development. Here’s how to set up security and access rights in your Odoo module:

1. Creating Security Groups

  • Purpose: Security groups are used to define different user roles within your module, each with its own set of permissions.
  • Implementation: Create groups in your module’s XML files. For example, define a group for basic users and another for administrators, each with different access levels.
  • Assignment: Assign these groups to users as needed. This determines what each user can see and do within your module.

2. Access Control Lists (ACLs)

  • Overview: ACLs are used to set permissions at the model (or object) level. They define what operations (create, read, update, delete) can be performed by a user group on a specific model.
  • Configuration: Configure ACLs in the CSV or XML file within your module. Specify the model, group, and the permissions for each operation.
  • Best Practices: Be cautious with permissions. Granting too many privileges can lead to security risks, while too few can hamper user experience.

3. Record Rules

  • Functionality: Record rules provide a finer level of control by governing access to individual records within a model.
  • Usage: Use them to restrict or allow access to specific records based on certain conditions. For instance, a user may only access records they created or records belonging to their department.
  • Setting Up: Define record rules in your module’s XML files. They often use domain filters to specify which records are accessible.

4. Field Level Security

  • Concept: Field level security allows you to restrict access to certain fields within a model. This is useful for sensitive data like pricing or personal information.
  • Implementation: In Odoo, you can set field-level security in the Python code of your model using attributes like ‘groups’ in field definitions.
  • Example: You could specify that only members of the ‘Manager’ group can view and edit the ‘salary’ field of an employee model.

Step 12: Debug and Test

After developing your Odoo module, it’s crucial to thoroughly debug and test it to ensure its reliability and effectiveness. This step is about identifying and fixing bugs, ensuring the module performs as expected, and confirming that it integrates well with Odoo’s ecosystem. Here’s how to approach debugging and testing in Odoo module development:

1. Using Odoo’s Built-in Tools

  • Debug Mode: Enable Odoo’s debug mode to access advanced features and detailed log outputs. This can be done by appending ‘?debug=1’ to the URL when accessing your Odoo instance.
  • Log Outputs: Odoo provides extensive logging capabilities. Monitor the logs for any warnings or error messages that occur when your module is running.
  • Developer Tools: Use the developer tools in the Odoo web interface for inspecting views, workflows, and other module components.

2. Unit Testing

  • Purpose: Unit tests are crucial for ensuring that each part of your module functions correctly on its own.
  • Tools: Utilize the Python standard library’s ‘unittest’ framework or Odoo-specific tools like ‘odoo.tests.common’ to create your tests.
  • Best Practices: Write tests for critical functions and common use cases of your module. Run these tests after any significant changes to ensure ongoing stability.

3. Testing User Interface

  • Manual Testing: Manually interact with your module through the Odoo web interface. This helps ensure that the user interface behaves as expected and that user interactions trigger the correct functionalities.
  • Scenario Testing: Simulate common user workflows to verify that your module integrates well with other Odoo components and that it handles data correctly.

4. Error Handling

  • Monitoring Errors: Keep an eye out for any errors reported in the Odoo logs or during testing. These might include syntax errors, runtime exceptions, or logical errors in your code.
  • Resolving Issues: Analyze the context and details of each error. Modify your code to handle these errors gracefully and prevent them from recurring.
  • Continual Testing: After fixing errors, retest your module to ensure that the fixes are effective and that no new issues have been introduced.

Basic Views 

Views govern how a model’s records are displayed. Each view type provides a different way of visualization (a list of records, a graph of their aggregate, etc.). Views can be requested either generically via their type (for example, a list of partners) or particularly via their id. The view with the correct type and the lowest priority will be used for generic requests (thus the lowest-priority view of each type is the default view for that type).

View inheritance enables you to change views declared elsewhere (add or remove material).

1. Generic View Declaration

A view is defined as an instance of the model ir.ui.view. The root element of the arch field implies the view type:

<record model="ir.ui.view" id="view_id">
    <field name="name">view.name</field>
    <field name="model">object_name</field>
    <field name="priority" eval="16"/>
    <field name="arch" type="xml">
        <!-- view content: <form>, <tree>, <graph>, ... -->
    </field>
</record>

2. Tree Views

Tree views, also known as list views, show records in a tabular format.

Their primary element is <tree>. The most basic tree view just lists all of the fields to be displayed in the table (each field as a column):

<tree string="Idea list">
    <field name="name"/>
    <field name="inventor_id"/>
</tree>

3. Form Views

Forms are used to generate and modify individual records.

Their primary element is <form>. They are made up of high-level structure elements (such as groups and notebooks) as well as interactive elements (such as buttons and fields):

<form string="Idea form">
    <group colspan="4">
        <group colspan="2" col="2">
            <separator string="General stuff" colspan="2"/>
            <field name="name"/>
            <field name="inventor_id"/>
        </group>

        <group colspan="2" col="2">
            <separator string="Dates" colspan="2"/>
            <field name="active"/>
            <field name="invent_date" readonly="1"/>
        </group>

        <notebook colspan="4">
            <page string="Description">
                <field name="description" nolabel="1"/>
            </page>
        </notebook>

        <field name="state"/>
    </group>
</form>

Form views can also use plain HTML for more flexible layouts:

<form string="Idea Form">
    <header>
        <button string="Confirm" type="object" name="action_confirm"
                invisible="state != 'draft'" class="oe_highlight" />
        <button string="Mark as done" type="object" name="action_done"
                invisible="state != 'confirmed'" class="oe_highlight"/>
        <button string="Reset to draft" type="object" name="action_draft"
                invisible="state not in ['confirmed', 'done']" />
        <field name="state" widget="statusbar"/>
    </header>
    <sheet>
        <div class="oe_title">
            <label for="name" class="oe_edit_only" string="Idea Name" />
            <h1><field name="name" /></h1>
        </div>
        <separator string="General" colspan="2" />
        <group colspan="2" col="2">
            <field name="description" placeholder="Idea description..." />
        </group>
    </sheet>
</form>

4. Search Views

The search field associated with the list view (and other aggregated views) can be customized using search views. Their root element is <search>, and they are made up of fields that specify which fields can be searched on:

<search>
    <field name="name"/>
    <field name="inventor_id"/>
</search>

If no search view for the model exists, Odoo creates one that only allows searching on the name field.

There is also a primary method for developing a custom module in Odoo. In this have to include an icon in the customized module. Simply create a Static folder within your module.

Create a folder called src within this folder, then another folder called “images” within this folder, then save your image as icon.png. This image will then be displayed in Odoo as the icon for your custom module.

Now, in Odoo, generate an ir.model.access.csv file for the model so that one can add, edit, or delete a record from it.

With all these steps, the creating module is complete and ready to be installed.  

Why Employ Emizen Tech for Odoo Services?

EmizenTech, as an authorized Odoo partner, stands out as a preferred choice for Odoo services due to the best solution as per the business needs. We have a team of experts specializing in Odoo development; the company provides a range of services, including module development, integration, and ongoing support. We focus on quality assurance and on-time delivery, making sure to fulfill all the client’s requirements. 

To know more, you can contact our experts 24*7 and get your module today. 

Conclusion 

Thus, by creating one, you can open up multiple possibilities for customization and enhance the functionality of your Odoo. In this blog, we have covered all the steps of “ How to create an Odoo module.”  we have explored the step-by-step process of developing a module, from setting up the development environment to implementing custom models and views. 

By following the steps in the blog, you can ensure that your Odoo module is well-organized, efficient, and easily maintainable and that development is done successfully.  

Avatar photo
Author

CTO at Emizentech and a member of the Forbes technology council, Amit Samsukha, is acknowledged by the Indian tech world as an innovator and community builder. He has a well-established vocation with 12+ years of progressive experience in the technology industry. He directs all product initiatives, worldwide sales and marketing, and business enablement. He has spearheaded the journey in the e-commerce landscape for various businesses in India and the U.S.

whatsapp