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
- 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,
- __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,
So this is how our __manifest__.py file will look like,
- 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.
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.
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,
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.
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:
- Flectra Docs :- https://doc.flectrahq.com/index.html
- Install Flectra in Ubuntu 16.04 :- https://www.oodlestechnologies.com/blogs/How-to-install-flectra-in-ubuntu-16.04