Enable Dark Mode!
how-to-create-a-module-in-odoo-16.jpg
By: Sahla Sherin

How to Create a Module in Odoo 16

Functional Odoo 16

The Odoo module is used to add new business logic or improve existing business logic. Now we are going to discuss how to create a new basic Odoo module in Odoo 16.

An Odoo module has a structure to keep the elements inside a module. As we already discussed, the structure of the Odoo module is in another blog.

Link: An Overview of Module Structure in Odoo 16

To create a module, we can use the scaffold command. This command automatically creates an empty module.

./odoo-bin scaffold module_name custom_addons_location

As I said above, we need to create a manifest file that contains two init files. To contain the Python models, we have to create a directory called models

Eg:

class GymMembership(models.Model):
   _name = "gym.membership"
   _inherit = ["mail.thread", "mail.activity.mixin"]
   _description = "Gym Membership"
   _rec_name = "reference"
   reference = fields.Char(string='GYM reference',required=True,readonly=True, default=lambda self: _('New'))
   member_id = fields.Many2one('res.partner', string='Member', required=True,tracking=True,
   domain="[('gym_member', '!=',False)]")
   membership_scheme_id = fields.Many2one('product.product',
                                       string='Membership scheme',
                                       required=True, tracking=True,
                                       domain="[('membership_date_from', '!=',False)]")
   paid_amount = fields.Integer(string="Paid Amount", tracking=True)
   membership_fees = fields.Float(string="Membership Fees", tracking=True,
                                  related="membership_scheme_id.list_price")
   sale_order_id = fields.Many2one('sale.order', string='Sales Order',
                                   ondelete='cascade', copy=False,
                                   readonly=True)
   membership_date_from = fields.Date(string='Membership Start Date',
                                      related="membership_scheme_id.membership_date_from",
                                      help='Date from which membership becomes active.')
   membership_date_to = fields.Date(string='Membership End Date',
                                    related="membership_scheme_id.membership_date_to",
                                    help='Date until which membership remains active.')
   company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True,
                                default=lambda self: self.env.company)

Here we import the needed modules and packages. Now we get a table and fields to store the value.

Add this to the init file

From . import membership

And also the model to the init of the module directory.

from . import models

To get a user interface, we need to define the views for that Odoo using XML files. So we have to define the menu and submenu. Gym Management menu and Membership submenu. 

Eg:

<menuitem id="gym_management_root"
         name="Gym Management"
         sequence="10"/>
<menuitem
       id="menu_gym_membership"
       name="Membership"
       parent="gym_member_root"
       action="action_gym_membership"
       sequence="30"/>

Now we can define the views like kanban, list, form, etc in the XML file. Currently, here we are defining the form and tree. Also, here we initially define the tree view and form view.

<record id="view_membership_tree" model="ir.ui.view">
   <field name="name">gym.membership.tree</field>
   <field name="model">gym.membership</field>
   <field name="arch" type="xml">
       <tree default_order="reference desc">
           <field name="reference"/>
           <field name="member_id"/>
           <field name="membership_scheme_id"/>
           <field name="membership_fees"/>
           <field name="state"/>
       </tree>
   </field>
</record>
<record id="view_membership_form" model="ir.ui.view">
   <field name="name">gym.membership.form</field>
   <field name="model">gym.membership</field>
   <field name="arch" type="xml">
       <form>
           <header>
               <field name="state" widget="statusbar"
                      options="{'clickable':'1'}"/>
           </header>
           <sheet>
               <div class="oe_title">
                   <h1>
                       <field name="reference"/>
                   </h1>
               </div>
               <group>
                   <group>
                       <field name="member_id"/>
                      <fieldname="membership_scheme_id"/>
                       <field name="paid_amount"/>
                   </group>
                   <group>
                       <field name="membership_fees"/>
                       <field name="sale_order_id" readonly="1"/>
                       <label for="membership_date_from" string="Membership Duration"/>
                       <div class="o_row">
                           <field name="membership_date_from" required="1"/>
                           -
                           <field name="membership_date_to" required="1"/>
                       </div>
                   </group>
               </group>
           </sheet>
           <div class="oe_chatter">
               <field name="message_follower_ids"/>
               <field name="activity_ids"/>
               <field name="message_ids"/>
           </div>
       </form>
   </field>
</record>

If you need, then you can add a security file, group, record rule, etc. According to your requirements. These files are used for access rights.

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_gym_membership_member,account.gym.membership,model_gym_membership,gym_mgmt_system.group_gym_member,1,0,0,0

To know more about access right https://www.cybrosys.com/blog/security-groups-access-rights-in-odoo-16

Add the path of this module in the manifest file, go-to apps, and upgrade, and then our custom module is there in the apps.

How to Create a Module in Odoo 16-cybrosys

Install the module, and now you can find this module on the menu.

How to Create a Module in Odoo 16-cybrosys

Now you can see the parent menu in the Gym Management. Click on that. Then you get the submenu member and other defined submenus.

How to Create a Module in Odoo 16-cybrosys

Here are the defined fields. You can fill in the details accordingly. 

How to Create a Module in Odoo 16-cybrosys

If you have made any changes in the XML file, then don’t forget to upgrade the module. 

How to Create a Module in Odoo 16-cybrosys

Click on this, and you will get the option to upgrade. If you have done any updates in Python, don’t forget to restart the service.

How to Create a Module in Odoo 16-cybrosys

If you want to add Icon for the module, then add icon.png in static->description->icon.png

In module info, you get details of the module given in the manifest file.

How to Create a Module in Odoo 16-cybrosys

Like this, you can create and alter modules in Odoo 16. 


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message