Create a module¶
Before creating your first module, it is necessary to create an Odoo.sh project and knowing your GitHub repository’s URL.
Glossary
~/srcis the directory where the Git repositories related to your Odoo projects are located.odoois the GitHub user.odoo-addonsis the GitHub repository.feature-1is the name of a development branch.mainis the name of the production branch.my_moduleis the name of the module.
Replace these as necessary.
Creare il ramo di sviluppo¶
From the Branches view:
In the branches navigation panel, click the + (New development branch) button next to Development.
Under Fork, select the
mainbranch.Under To, enter
feature-1.
Once the build is ready, you can access the editor and
the code of your development branch from the folder ~/src/user folder.
Clone your GitHub repository on your computer by running the following commands:
mkdir ~/src cd ~/src git clone https://github.com/odoo/odoo-addons.git cd ~/src/odoo-addons
Create a new branch by running:
git checkout -b feature-1 main
Creare la struttura del modulo¶
Scaffolding¶
While it is not required, scaffolding avoids the tedium of setting the basic Odoo module structure.
You can scaffold a new module using the odoo-bin executable.
From the editor terminal, run:
odoo-bin scaffold my_module ~/src/user/
With Odoo installed on your computer, run:
./odoo-bin scaffold my_module ~/src/odoo-addons/
Suggerimento
If you do not want to install Odoo on your computer, you can also download this module
structure template. Replace every occurrence of my_module with
the name of your choice.
The structure below will be generated:
my_module
├── __init__.py
├── __manifest__.py
├── controllers
│ ├── __init__.py
│ └── controllers.py
├── demo
│ └── demo.xml
├── models
│ ├── __init__.py
│ └── models.py
├── security
│ ├── ir.model.access.csv
│ └── models.py
└── views
├── templates.xml
└── views.xml
Avvertimento
Only use alphanumeric characters (a-z, 0-9) or underscores (_) when naming your module, as
its name is used for Python classes, and class names containing special characters other than
underscores are not valid in Python.
Uncomment the following files:
models/models.pyan example of a model with its fieldsviews/views.xmla tree and a form view, with the menus opening themdemo/demo.xmldemo records for the example modelcontrollers/controllers.pyan example of a controller implementing some routesviews/templates.xmltwo example qweb views used by the controller routes__manifest__.pythe manifest of your module, including its title, description and data files to load. Uncomment the access control list data file:# 'security/ir.model.access.csv',
Manuale¶
To create your module structure manually, follow the Server framework 101 tutorial to understand the structure of a module and the content of each file.
Push on the development branch¶
Stage the changes to be committed by running:
git add my_module
Commit your changes by running:
git commit -m "My first module"Push your changes to your remote repository by running:
From the editor terminal, run:
git push https HEAD:feature-1
Run:
git push -u origin feature-1
It is only necessary to specify
-u origin feature-1the first time you push. After that, run:git push
Test the module¶
The branch should appear under the Development section of the Branches view navigation panel.
Click the branch name to view its history, including the changes you just pushed. Once the database is ready, click Connect to access it.
If your Odoo.sh project is configured to automatically install the module, it will appear directly on the database dashboard. Otherwise, it will be available for installation under the Apps app.
Testare con i dati di produzione¶
Nota
A production database is required for this step. If you do not have one yet, create it.
Once you have tested the module in a development build with the demo data and believe it is ready, you can test it with the production data using a staging branch.
You can:
Convert your development branch to a staging branch by dragging and dropping it into the Staging section.
Merge it into an existing staging branch by dragging and dropping it onto that branch.
Use the
git mergecommand to merge your branches.
This creates a new staging build that duplicates the production database and runs it on a server updated with your branch’s latest changes.
Once the database is ready, click Connect to access it.
Install the module¶
Install the module from the Apps app. As the module may not appear directly in the list of apps, update the list of apps by activating the developer mode and clicking .
Nota
The module is not installed automatically, as the purpose of the staging build is to test the behavior of your changes as they would be on the production database, so you would not want a module installed automatically.
Distribuire in produzione¶
Once you have tested the module in a staging branch with the production data and believe it is ready for production, you can merge your branch into the production branch by:
Dragging and dropping the staging branch onto the production branch.
Using the
git mergecommand to merge your branches.
This merges the latest changes from the staging branch into the production branch and updates the production server with them.
Once the database is ready, click Connect to access it.
Install the module¶
Install the module from the Apps app. As the module may not appear directly in the list of apps, update the list of apps by activating the developer mode and clicking .
Aggiungere una modifica¶
This section explains how to add a change in your module by adding a new field in a model and deploying it.
From the editor or from your computer, open the module folder
~/src/odoo-addons/my_module, then open themodels/models.pyfile to edit it. After the description field:description = fields.Text()
add a datetime field:
start_datetime = fields.Datetime('Start time', default=lambda self: fields.Datetime.now())
Open the
views/views.xmlfile and after:<field name="value2"/>
add:
<field name="start_datetime"/>
These changes alter the database structure by adding a column to a table and modifying a view. To be applied to an existing database, such as your production database, these changes require the module to be updated. If you would like the update to be performed automatically by the Odoo.sh platform when you push your changes, increase the module’s version in its manifest by opening
__manifest__.pyand replacing:'version': '0.1',
with:
'version': '0.2',
The platform will detect a version change and trigger the module update upon deployment of the new revision.
Next, push the changes.
Once you have tested your changes, you can merge them into the production branch, for instance, by dragging and dropping the branch onto the production branch in the Odoo.sh interface. Since you increased the module version in the manifest, the platform will automatically update the module, and your new field will be available immediately. Otherwise, you can manually update the module within the apps list.
Utilizzare una libreria Python esterna¶
If you would like to use an external Python library that is not installed by default, you can define
a requirements.txt file listing the external libraries your modules depend on. The platform
will use this file to automatically install the Python libraries your project needs.
Nota
It is not possible to install or upgrade system packages on Odoo.sh databases (e.g., apt packages). However, under specific conditions, packages can be considered for installation. This also applies to Python modules that require system packages for compilation and to third-party Odoo modules.
PostgreSQL extensions are not supported on Odoo.sh hence it is not possible to install extensions (such as PostGIS, ltree, …) in Odoo.sh databases.
For example, to use the Unidecode library in your module:
Create a file
requirements.txtin the root folder of your repository:From the Odoo.sh editor, create and open the file
~/src/user/requirements.txt.From your computer, create nd open the file
~/src/odoo-addons/requirements.txt.
Add to the file:
unidecode
You can now use the library in your module, for instance, to remove accents from characters in the name field of your model. To do so, open the
models/models.pyfile and before:from odoo import models, fields, api
add:
from unidecode import unidecode
After:
start_datetime = fields.Datetime('Start time', default=lambda self: fields.Datetime.now())
add:
@api.model def create(self, values): if 'name' in values: values['name'] = unidecode(values['name']) return super(my_module, self).create(values) def write(self, values): if 'name' in values: values['name'] = unidecode(values['name']) return super(my_module, self).write(values)
Increase the module version to install the Python dependency by editing the module’s manifest
__manifest__.py.Then, push the changes.
Suggerimento
Stage the
requirements.txtfile by runninggit add requirements.txt.