How to Create a Module in Odoo 16 Step by Step

Here, we will discuss how to create a new module in Odoo 16 step by step. Before creating a module, you must have installed Odoo with addons path and know how of python programming. You can add multiple locations using a comma in addons path. Click here, if you don't know how to install Odoo.

How to Create a Module in Odoo 16

Here the steps to create a module in Odoo 16,


Structure of Odoo Module


  • Python Classes in Python Files
  • Object Views for UI
  • Data Files for static data
  • Security FIles (For Access Rights & Rules)
  • _init_.py To include python file
  • _manifest_.py to show description and to include UI files.


Manifest file


This file “__manifest__.py” helps to specify the metadata of the module and to show the module inside the Odoo app list.

Example
{
'name': 'Module Name',
'version': '16.0.1.0.0', 
'category': '', 
'author': 'Author Name', 
'website': "https://openerpodoo.blogspot.com/", 
'company': 'My Company', 
'summary': 'Record Student Details', 
'description': """ To manage School""", 
'depends': ['base'], 
'data': [ ], 
'qweb': [], 
'license': 'AGPL-3', 
'installable': True, 
'auto_install': False, 
}

In _manifest_.py file, name is module name, description contains the module long description with details, depends contains the list of dependent modules, data contains the list of dependent view/xml files and there are many more.

Init File

Next, we move on to __init__.py In the main __init__.py, we have to import all the directories from the custom module, which contains python files. The main init is given below, 
from . import models 
In above line, models is a python file. If you have any python type like module.py and you want to add here then you can add here like this from . import module. You don’t need to add python extension (which is .py) here.

Python File

In python file, you have to create python files with .py extension to add python code as below,

from odoo import models, fields 

class PlayerName(models.Model): 
    _name = 'player.details' 
    
    name = fields.Char(string='Name', required=True) 
    age = fields.Integer(string='Age') 
    photo = fields.Binary(string='Image') 
    gender = fields.Selection([('male', 'Male'), ('female', 'Female')], string='Gender)
    player_dob = fields.Date(string="Date of Birth")

UI File (XML)

        <!--Player Details Tree View-->
<record id="player_detail_form_view" model="ir.ui.view"> 
<field name="name">player_detail_form_view</field> 
<field name="model">player.details</field> <field name="arch" type="xml"> <form string="Player"> <sheet>
<group string="Player Details"> 
    <group> 
        <field name="name"/> 
        <field name="photo"/>
        <field name="gender"/> 
    </group> 
    <group> 
        <field name="age"/>
        <field name="student_dob"/>
    </group> 
</group>
</sheet> 
</form> 
</field> 
</record> 
        <!--Player Details Tree View--> 
<record id="player_detail_tree_view" model="ir.ui.view"> 
<field name="name">player_detail_tree_view</field> 
<field name="model">player.details</field> <field name="arch" type="xml"> <tree>     <field name="name"/>     <field name="age"/>     <field name="student_dob"/>         <field name="gender"/>
</tree> </field> </record>
         <!--Player Details Action Window-->
<record id="player_detail_act_window" model="ir.actions.act_window">
<field name="name">Player Details</field> <field name="res_model">player.details</field> <field name="view_mode">tree,form</field> </record>
        <!--Main Menu Player Details --> 
<menuitem id="player_detail_main_menu" name="Players" sequence="7"/> 

        <!--Menu Player Details-->
<menuitem id="player_detail_menu_item" name="Player Detail" parent="player_detail_main_menu" 
action="player_detail_act_window" sequence="0"/> 
</data> 
</odoo>

How to Create Access Rights in Odoo

To create access rights in Odoo, you have to create a csv file in your module named "ir.model.access.csv". In which you will declared your python class/model. Here we provide a sample as below,

Syntax
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink player_detail_records,player_detail,model_player_detail,,1,1,1,1

id =>    An unique id should be created for csv
name =>    Name of access right 
model_id:id =>    Name of model to give access
group_id:id =>   user who have this group can access this model 
perm_read =>    user can read the records of this model
perm_write =>    user can write and update the records of this model
perm_create =>    user can create the records of this model
perm_unlink =>    user can delete the records of this model

Except these, you can add images and menu icons too. Now just have to restart your server and update the app list. Your "player_details" will be show in the app list, where you can install and run it. 

Here we shared a sample of module creation in Odoo. We hope this article will helpful for you to create a module in Odoo step by step.
Previous Post Next Post