Create a Custom Module in Flectra

Posted By : Piyush Khandelwal | 20-Dec-2019

For those who are in the starting stage of Flectra development, It is a tough task to develop a custom module. In this section, we will learn how to create a custom module in the Flectra.

Composition of a module

A Flectra module can contain a number of elements:

  • Business objects(Models)
    • Declared as Python classes, these resources are automatically persisted by Flectra based on their configuration.
  • Data files(Views)
    • XML or CSV files declaring views or reports, configuration data (modules parameterization), demonstration data and more.
  • Web controllers
    • Handle requests from web browsers.
  • Static web data
    • Images, CSS or javascript files are used by the online interface or web site.

Module Structure

  • Flectra also provides a mechanism to help set up a new custom module, flectra-bin has a subcommand scaffold to create an empty module.The command creates a subdirectory for your module, and automatically generates a bunch of standard files for a module. Most of them simply contains commented code
    • flectra-bin scaffold <module name> <where to put it>
  • These are the above mentioned standard files.
    • __init__.py  :- In this file we have to import all the python files that we are going to use. Suppose we have a python file called model.py in our module. The first thing we’ll do is import the model.py in the __init__.py file.

So our __init__.py file will be like this, 

import model

  • __manifest__.py :-  In this file, We mention the name of the Module and the Author, Module Version, Description, Category etc. Now let us see what all these things are for,

Name – Module name to be displayed.

Author – The name of the author of the module.

Version – Version of the released module.

Website – The Website Address of the Company

Category – Category of the module, Whether it is sales, purchase etc.

Depends – If our module depends on any of the other modules, we have to mention that name in the depends. As we are going to create a custom module and it is not currently depending on any of the other modules, just add depends as base.

Data – In the data section, we specify all the .xml files. In our case, we will mention the view.xml here.

So this is how our  __manifest__.py file will look like,

{

    'name': ‘Student Info',

    'summary': """This module will store student details. """,

    'description': """This module will store student details.""",

    'version': '1.0.0',

    'author': ‘Piyush Khandelwal',

    'company': 'Oodles Technologies',

    'website': 'https://technologies.oodles.io/',

    'category': 'Tools',

    'depends': ['base'],

    'data': [

        'view.xml',

    ],

    'demo': [],

    'installable': True,

    'auto_install': False,

}

  • models.py :- In this file, we create a new model class to store the values of the student, let it be ‘student.student_info’ . After creating a new model class, a table will get generated in the database. Inside the model, we declare all the fields that we are going to use in this table.

from flectra import models, fields

class Student(models.Model):

    _name = "student.student_info"

    name = fields.Char(string='Full Name')

    photo = fields.Binary(string='Photo')

    age = fields.Integer(string='Age')

    dob = fields.Date(string="Date of Birth")

    gender = fields.Selection([('MALE', 'Male'), ('FEMALE', 'Female'), ('OTHER’', 'Other')],)

First, We have to import the models and fields from flectra, Then we have to define a class that inherits from models.Model with the database table name in the _name field. In our case, It’s ‘student.student_info’. Next, we have to define fields in this table. Ex:- name,age etc.

  • views.xml :- Views define the way the records of a model are displayed on the user interface. Each type of view represents a mode of visualization (a list of records, a graph of their aggregation, …). We have created a table in the database, now we need to define how and under which menu-item it should be in the user interface.

Now we are going to define a menu-item and an action record for that menu-item.

 

<?xml version="1.0" encoding="UTF-8"?>

<flectra>

    <data>

# Parent MenuItem

        <menuitem id="menu_student" # ID of the menuitem

name="Student"/>   # name of the menuitem to be shown on the UI.

# Child MenuItem , assigning parent menu item by parent="menu_student"

        <menuitem id="menu_student_info" # ID of the menuitem.

name="Student Info" # name of the menuitem to be shown on the UI.

parent="menu_student" # parent_id.

action="action_student_info"/> # action id.

 

#Action for the child menu-item

<record model="ir.actions.act_window" id="action_student_info">

            <field name="name">Student Info</field># name of the action

            <field name="res_model">student.student_info</field> #name of the model

            <field name="view_mode">tree,form</field># type of views

            <field name="domain">[]</field>

            <field name="help" type="html">

                <p class="o_view_nocontent_smiling_face">Create new student

                </p>

            </field>

</record>

    </data>

</flectra>

Now, that we have created two menu-items and assigned an action on the child menu-item with parent = “menu_student”, Flectra will create a menu and a sub menu in UI for the parent and child menu items. In the action, we have assigned which type of view should be there in the ‘view_mode’.Ex: tree,form etc .

Now we have to create form and tree view for the model,

# Tree View

<record id="student_info_tree_view" model="ir.ui.view"> # tree id 

            <field name="name">student.student_info.tree</field> # name of the view

            <field name="model">student.student_info</field># Model name

            <field name="arch" type="xml">

                <tree string="Student">

                    <field name="name" />

                    <field name="gender" />

                    <field name="age" />

                    <field name="dob" />

                </tree>

            </field>

</record>

 

# Form View

 

<record id="student_info_form_view" model="ir.ui.view">

            <field name="name">student.student_info.form</field>

            <field name="model">student.student_info</field>

            <field name="arch" type="xml">

                <form string="Student">

                    <sheet>

<field name="photo" widget="image" class="oe_left oe_avatar" />

<group col=”4”>

           <field name="name" />

           <field name="gender" />

           <field name="age" />

           <field name="dob" />

</group>

</sheet>

</form>

</field>

</record>


 

By using sheet tag, a sheet will appear inside the form, it will make the form more beautiful.

IMP: If the group tag are not given then the ‘string’ parameter for the field given in the model.py will not get displayed in the form view.

The module is now completed, make sure that the custom module is inside the proper add-ons path. Then Go to flectra, activate developer mode. Then Apps -> update apps list -> click on update.The technical name of our module is the folder name and the name is Student Info which is given in the manifest file.

Now after updating the apps list, you can search for the module based on either of those names.

Conclusion

You have now learnt about “How to create custom module in Flectra”. If you'd like to learn more about them, please visit the following links:

 

About Author

Author Image
Piyush Khandelwal

Piyush is a bright web developer. He is Bachelor in Computer Applications. He has good knowledge of Python,Odoo, Flectra and Django Framework.

Request for Proposal

Name is required

Comment is required

Sending message..