Creating a Custom Module using Scaffold in Odoo
Odoo is a suite of business management tools covering all aspects of business operations. Odoo has many built-in modules like CRM, sales, purchase inventory, etc which are very useful for various applications and make our task easier. But, in a certain scenario, we may have to create a custom module for our particular purpose. For that Odoo’s modular structure provides us with the freedom to create custom modules according to our needs. Let us dive into the creation of a custom module in Odoo.
Ensure that Odoo is installed and is configured with IDE to work with. We need a developing environment and that is the reason we are configuring Odoo with PyCharm. We are going to create a school management module in Odoo. So go to PyCharm and as we have configured with Odoo, several of the Odoo files can be seen. We have to create a database for our module and set the corresponding username and password. With Scaffolding, we can create the module structure automatically helping us to save our time & efforts.
The module name is given as school_management and the corresponding path is given. Once created we will get the directory as a school under the custom directory,
We can see many subdirectories under the school_management and two python files.
- __init__.py file :
This file is used as a python file descriptor. This helps to get the python files inside the directories. This is file is needed to be created inside the subdirectories also. Whatever the directory created and py files, we have to define inside the __init__.py file, only then it will automatically load those files into Odoo.
We can see the controllers, wizards, models, and reports are defined inside the __init__.py. All python files have to be defined and we can see that the subdirectories also have an __init__.py file.
- __manifest__.py :
This contains the metadata information of the module like name, author name, description, etc. We have to mention all the XML files inside this. All the information regarding the module dependencies will be presented here.
By looking at the manifest file, one can understand the module detail, the purpose of the module, what all are dependencies used, the author, website, etc.
Here we can see that the name, summary, description, author, website, category, version are given. Also, any modules which are used in this custom module are listed. The files like view files, security access files are also defined inside the manifest. After including the dependencies and files in __manifest__.py, save and run the server and go to the Odoo server and upgrade the module. From Apps, we can see our school management module. We can install the module by clicking the three dots. Now our custom module is added
Once installed we can see it in the app lists.
Now we have a created and installed module. But nothing is defined inside the module. So we have to define a model for our School Management module.
For that go to the models directory- we can create a new file models.py and in that, we are defining our model for school management. Either we can create a new model or inherit from existing base models.
So go to models.py and create fields according to the module need.
Here we have defined a model school.management with three fields. And we have to define the corresponding view for these fields which show how a user can view it and is a written views.xml file. Three fields are given in the character type.
This views.xml file has to be added to the __manifest__.py file.
This is code the form view for the school management and we can see how this will be displayed to the users in Odoo. Before going to run the server, one thing should be made sure, i.e to give access to this model in the security file
Access should be given to the model and the permissions for read, write, create and delete are given as 1,1,1,1. When the server is run, we can see the form view from the odoo.
Here the fields for name, roll number, and the class of students are shown. We can also define the corresponding tree view for this form.
And the tree view will be,
This is the action record created for the menu and we have to call this action under the menu as below,
We can see our main menu is School Management and from that, we are giving a submenu.
When we click the Student Records we will get the corresponding student record view.
This is how we create a custom module and we can add different views to it according to our need.
If you are having queries or need any assistance in Odoo, reach out to us on our website www.infintor.com