+91 9818211679, +91 9998040067       [email protected]

How To Create a Custom Module in Odoo

 

In this blog, we‘ll be focusing on how we can create a custom module in odoo. Morever,we'll also discuss how we can design the view in this module.

In Odoo Development, Odoo Modules can either add brand new business logic to an Odoo System or alter and extend existing business logic. A module can be created to add your country’s accounting rules to Odoo’s generic accounting support, while the next module adds support for real-time visualization of a bus fleet.


Everything in Odoo thus starts and ends with modules.

 

Structure of a Module:

 

An Odoo module can contain many elements like:

 

Objects/Models :Declared as Python classes, these resources are automatically persisted by Odoo based on their configuration

 

View Files: XML or CSV files declaring metadata (views or reports), configuration data (modules parameterization), demonstration data and more

 

Controllers: Are used to handle requests from web browsers

 

Static Web Data: Images, CSS or JavaScript files used by the web interface or website

 

In this case, we‘ll be focusing on how to generate a simple custom module using python models & xml views. So, let us start first by naming our custom module.

 

We can name this custom module whatever we want, but it should be related to the functioning of the module so that, it is user-friendly as well.

 

In this case, we are creating a module based on an e-commerce shop. So, let us name this folder as electronics shop.

create custom module in odoo

This folder(module) will contain all our models & data files.


Now we need to create some sub-folders inside this module. The basic folders that can be used in a module are :

   Controllers

   Data

   Models

   Report

   Security

   Static

   Views

   Wizard


Now we need to keep  in mind that for folders containing .py file, we need to create an __init__.py file inside our module and those folders also. The  __init__.py file is used so that these .py files can be called first. So we need to create an __init__.py file inside our module.

Also, we need to create an __manifest__.py file inside our module. This manifest file as the name suggests is a declaration for our module about its general information and is also used to call view/CSV/static files of the module.

We are not using controllers, report, wizard in this case. So the final structure of the module is something like this:

create custom module in odoo

Now we‘ll discuss the structure of the __init__.py and __manifest__.py file.

The __init__ file is present inside our main module, and it is used for calling the below folders:      

   controllers

   models

   wizard

The structure of our __init__.py file is something like this:

create custom module in odoo

Now let us discuss about the structure of our __manifest__.py file:

create custom module in odoo

In the above image as you can see, we have declared the following parameters:

   Name : Name of the module to be displayed.

   Version: A version of the custom module with optional Odoo version mention

   Author: Name of the owner or the person who created the module

   Website: The web address of the company/person.

   Summary

   Description: About the functioning of the module

   Depends : Here we mention the name of the module on which our custom module depends.

   Data: In the data section, we have to specify all the .xml/.csv files here.

   Licence: Licence of the module.

   Installable, Application and auto-install.


Now all the outer files & folder‘s part is complete, so let‘s move forward to the basic core of our module. First of all, we need to create the __init__.py file inside our models‘ folder also to call the .py file from our model.


This __init__.py file will look something like this, here product.py is our main model file :

create custom module in odoo

Now let‘s move forward to our model file. Here we‘ll create a new model, or we can say a new table (in database terms) which will have fields(columns) for data entry.

There are various types of fields in Odoo to like:

   Character

   Integer

   Float

   Text

   Html

   Selection


and there are also some relational fields like Many2one, One2many & Many2many fields.

 

Here is a structure of our main model file, i.e., product.py file. So the file would look something like this.

create custom module in odoo

In the above file, first of all, we import packages that we are going to use in our file.


For example, the basic API, fields, models are mostly imported to use these inside the code. Any additional packages that you may want to use are also imported in the above file. 


In this case we have created some basic fields like name, price and also a selection field to select the product type and also a relational field which is the company field.


Now this relational field will connect us to the model mentioned inside the field syntax so that we get a list of the companies for our product and we can choose anyone from them.


You can also create a Sequential field which in other words is also an auto incremental field which generate an auto incremental number every time a new record is generated so that we can generate a new number for every record (as shown in the image below & code for how to create your own sequential field shown above in python code (api.model) and xml code for that is also shown below)

create custom module in odoo

This file needs to be put inside the data folder. And called inside your __manifest__.py file.


So this will generate a Sequence/Sequential Field like this:

create custom module in odoo

Let us move to the part where we‘ll create xml file so that these fields will be displayed in the view.

 

These xml files are very important to display our fields or table data at the user interface so that it is easily accessible to the user.

 

So we‘ll first create an xml file inside our views folder. The fields we mention inside this xml file are visible on the interface.


After creating this xml file, we need to make sure to call this xml file inside the manifest file by giving the path for this file.

 

There are different types of views through which these fields can be displayed on the interface.

 

We Can Define Different Type of Views for our Model like:

   Form view

   Tree/List view

   Kanban View

   Graph view

   Search view


In this case, we have created form, list & kanban view for our module.

 

FORM VIEW

Form view shows the page where a form is open of our model & its fields. Here you can enter data inside the fields.

create custom module in odoo
create custom module in odoo

The above code is of a form view. As the name suggests, this view provides us with the form of our model.

 

We can design it using various Html tags like groups, pages etc. as we like. Here you can also add a tag <notebook>, Notebook gives multiple tabs on a single page in Odoo like, in this case, it gives us two pages Description and Product Details. This view gives us a final result of a form like this:

create custom module in odoo

Now we can also create a Tree view for our model so that it gives a list view of our records.


TREE VIEW

Tree views can take additional attributes to customize their behavior further:

create custom module in odoo

The Tree or List View looks like this on the interface:

create custom module in odoo

KANBAN VIEW


A kanban view shows a set of cards possibly grouped in columns. Each card represents a record, and each column the values of an aggregation field.

create custom module in odoo

The above code will make a kanban view like this:

create custom module in odoo

SEARCH VIEW


A search view defines the search options that will be available in the views so that you can search in your records through this view based on the custom filters which you can add.

create custom module in odoo

After you have created all views in the XML file, now the question arises how you will access these views. We need to create a menu and action for our views to appear on the interface.

 

Actions and menus are regular records in the database, usually declared through data files. Actions can be triggered in three ways:

   by clicking on menu items (linked to specific actions)

   by clicking on buttons in views (if these are connected to actions)

   as contextual actions on the object

 

Because menus are somewhat complex to declare there is a <menuitem> shortcut to declare an ir.ui.menu and connect it to the corresponding action more easily.

create custom module in odoo

The above views are the action for our tree and form views.

 

Now we need to call these actions inside our <menuitem> so that when we click on our menu, these actions are called which will eventually call our views.

create custom module in odoo

This will create a parent menu named Electronic shop and a sub-menu named Products like this:

create custom module in odoo

There are the primary way to create a custom module in Odoo. You can also add an icon to your custom module. Just create a folder named Static inside your module. Inside this folder create another folder named src, inside this folder create another folder named “images“ and put your image as icon.png. This image will then be displayed as the icon of your custom module in Odoo.


Now in Odoo12, we need to create an ir.model.access.csv file for the model that we create, so that a user can add, edit or delete a record from that model. We can also create the record rules for our custom module, to limit the rights of a certain data to a particular user.


READ MOREHOW TO CREATE CSV FILE AND RECORD RULES.


Now the process of module development in odoo is complete, and our custom module is ready to be installed.


 If you have any query feel free to ask in the comment section or mail us at: [email protected] You can also contact us for Odoo Services.

There are no comments for now.