How To Make Custom Module In Odoo10

In this tutorial, i will guide you to create a custom module in Odoo 10. Odoo uses a client-server architecture in which Odoo is the server and the web browser are the clients, which access Odoo using RPC calls. Odoo Modules can either add new business logic or extend the already existing one. Odoo follows a MVC like architecture. And it can be explained as follows.

  • Model -- defines the structure.
  • View -- defines user interface.
  • Controller -- defines business logic.

This tutorial covers only basic knowledge of module creation. So no inheritance and other information are not included.

I have divided the module creation into five tiers for easy understanding.

Module Structure

Odoo follows specific module structure. For easy way to create a skeletal structure of the module you can use the scaffold command python odoo.py scaffold my_module /path. For more information you can check my guide on Odoo CLI commands here. This will create a basic structure of the module with some sample codes which are commented. Module name must be all small letters and have no space, you can use hyphen instead of space.

In Odoo 10 the descriptor file is named as __manifest__.py which can be founded at the route of the module, which describes the module information. In older versions this was named as __openerp__.py. You can use this name, but it is deprecated in the newer version.

If you created your module structure using scaffold command then there will be some sample data like as follows in __manifest__.py file.

                            
                            
{ 'name': "Title", 'summary': "Short subtitle phrase", 'description': """Long description""", 'author': "Vishnu Arukat", 'license': "AGPL-3", 'website': "http://www.example.com", 'category': 'Uncategorized', #all category can be seen in the base/module/module_data.xml 'version': '10.0.', 'depends': ['base'], 'data': ['views.xml'], 'demo': ['demo.xml'], }

In every module the two important files are __manifest__.py and __init__.py. The latter one is used to make the module importable by Odoo. You have to import every python files or folders conatining python files (route __init__.py)inside __init__.py. If you want to see what Odoo doing with the descriptor file, check this link. I will explain all the descriptor keywords in another tutorial. These two files must be present in your module.

Model Data

In this phase you have to create business logic files (python files) which contains different fields which are suitable for your application.Example is given below. The file location can be anywhere but i would suggest you to put in the folder models, either way you have to import it in the __init__.py.

Below is an example of a application data. For more information on the fields that i used in this example, you can check my tutorial on that here. Consider the file name as hotel_model.py.

                            
                            
# -*- coding: utf-8 -*- from odoo import models, fields, api class HotelManagement(models.Model): _name = 'hotel.manage' _description = 'Hotel Management' _order = 'date desc' _rec_name = "name" name = fields.Char(string='Table Name', size=100, translate=False, required=True) date = fields.Date('Date') customer_id = fields.Many2many('res.partner', string='Customers') remarks = fields.Text('Remarks') rate = fields.Selection([('bad', 'Bad'), ('average', 'Average'), ('good', 'Good')], string="Rate") description = fields.Html('Description', sanitize=True, strip_style=False, translate=False) picture = fields.Binary('Picture') take_away = fields.Boolean('Take Away ?') no_customers = fields.Integer('No Of Customers', default=0, help="Total No of customers") amount = fields.Float('Total', digits=(14, 2)) partner_ids = fields.One2many('res.partner','order_id',string="Customers") class ResPartner(models.Model): _inherit = "res.partner" order_id = fields.Many2one('hotel.manage', string="Order")

Add this file to the __init__.py like from . import hotel_model. Odoo models are python classes which is added in the central model registry,which is used for later usages by the another models.new models are defined using the _name ='model name' ,when inheriting a model we can use the _inherit ="model name".

Models have structural attributes that describes the model such as _name ,_description,_rec_name and _order which are as follows.

                            
                            
// Application Models
_name //this act as an internal global identifier for the module.
_description //for adding the human friendly tiltle for the module.
_order //to sort th record by a field can have multiple fields and also specify the order for the sorting like asc or desc.
_rec_name // usually the name is used to represent the model but you can use another field.

Now the business logic has been defined next is defining the view.

View Definition

There are many types of views in Odoo, right now, we only look at the form and tree views. Below is the example of the view definition. Any view that we add, has to be inside the following tag.

Position of the view files can be anywhere. My suggestion is to put it in the views folder. Consider the file name as hotel_view.xml.

                            
                            
// Put views inside this
<?xml version="1.0" encoding="utf-8"?> <odoo> <data> ""view definitions goes here"" </data> </odoo>
                            
                                
// Form view definition
<record id="hotel_manage_view_form" model="ir.ui.view"> <field name="name">Hotel Management Form</field> <field name="model">hotel.manage</field> <field name="arch" type="xml"> <form> <group> <field name="name"/> <field name="customer_id" widget="many2many_tags"/> </group> <group> <field name="date"/> <field name="remarks"/> </group> <group> <field name="rate"/> <field name="description"/> <field name="picture"/> <field name="take_away"/> </group> <group> <field name="no_customers"/> <field name="amount"/> </group> <group> <field name="partner_ids"/> </group> </form> </field> </record>
                            
                                
// Tree view definition
<record id="hotel_manage_view_tree" model="ir.ui.view"> <field name="name">Hotel Management Tree</field> <field name="model">hotel.manage</field> <field name="arch" type="xml"> <tree> <field name="name"/> <field name="date"/> <field name="amount"/> </tree> </field> </record>

After defining the views we need to add a menu and an action to that menu, only after that we can see the defined views in Odoo. Even if you don't define the tree or form view Odoo will create a default tree view for that model with name as default field.

                            
                            
// Menu definition
<menuitem id="hotel_manage_menu" name="Hotel" action="hotel_manage_action" parent="" sequence="5" />
// Action definition
<record id="hotel_manage_action" model="ir.actions.act_window"> <field name="name">Hotel Management</field> <field name="res_model">hotel.manage</field> <field name="view_mode">tree,form</field> <field name="target">current</field> <field name="limit">200</field> <field name="help" type="html"> <p class="oe_view_nocontent_create"> // message to show if no content </p> </field> </record>
// Action also can be defined like this
<act_window id="hotel_manage_action" name="Hotel Management" res_model="hotel.manage" />

After creating the views, add this file to the __manifest__.py in data with the relative path from module as route. Example 'data': ['views/hotel_view.xml'].

This will create menu called Hotel in the top menu bar of Odoo, because we didn't add any parent id in the menu definition. If we want to make it under a certain menu we can add the id of that menu as parent.

Deploying Module

Final phase is to install this in Odoo, for that put the created module in the addons path that you defined in the config file. Then restart the server ,activate the developer mode and go to the apps menu. Update the app list then search for the module that you created and click install.You can see the new Hotel menu in Odoo. Congratulation..! you have successfully created a custom module.

Extra References

For those who like to go further, I would like to refer you the Odoo reference docs. And if you want to check out the XML schemas for the Odoo you can go here and here.