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

  • ~/src is the directory where the Git repositories related to your Odoo projects are located.

  • odoo is the GitHub user.

  • odoo-addons is the GitHub repository.

  • feature-1 is the name of a development branch.

  • main is the name of the production branch.

  • my_module is the name of the module.

Replace these as necessary.

Den Entwicklungszweig erstellen

From the Branches view:

  • In the branches navigation panel, click the + (New development branch) button next to Development.

  • Under Fork, select the main branch.

  • Under To, enter feature-1.

Forking the production branch to create a development branch on Odoo.sh

Once the build is ready, you can access the editor and the code of your development branch from the folder ~/src/user folder.

Die Modulstruktur erstellen

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/

Tipp

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

Warnung

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.py an example of a model with its fields

  • views/views.xml a tree and a form view, with the menus opening them

  • demo/demo.xml demo records for the example model

  • controllers/controllers.py an example of a controller implementing some routes

  • views/templates.xml two example qweb views used by the controller routes

  • __manifest__.py the 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',
    

Manuell

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

  1. Stage the changes to be committed by running:

    git add my_module
    
  2. Commit your changes by running:

    git commit -m "My first module"
    
  3. Push your changes to your remote repository by running:

    From the editor terminal, run:

    git push https HEAD:feature-1
    

Test the module

The branch should appear under the Development section of the Branches view navigation panel.

Example of a development branch

Click the branch name to view its history, including the changes you just pushed. Once the database is ready, click Connect to access it.

The connect button to access a database

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.

The new module on the database dashboard

Mit den Produktionsdaten testen

Bemerkung

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 merge command 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 Update Apps List ‣ Update.

Bemerkung

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.

In Produktion einsetzen

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 merge command 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 Update Apps List ‣ Update.

Eine Änderung hinzufügen

This section explains how to add a change in your module by adding a new field in a model and deploying it.

  1. From the editor or from your computer, open the module folder ~/src/odoo-addons/my_module, then open the models/models.py file 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())
    
  2. Open the views/views.xml file and after:

    <field name="value2"/>
    

    add:

    <field name="start_datetime"/>
    
  3. 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__.py and 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.

  4. Next, push the changes.

  5. 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.

Eine externe Python-Bibliothek verwenden

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.

Bemerkung

  • 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:

  1. Create a file requirements.txt in 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.

  2. Add to the file:

    unidecode
    
  3. 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.py file and before:

    from odoo import models, fields, api
    

    add:

    from unidecode import unidecode
    
  4. 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)
    
  5. Increase the module version to install the Python dependency by editing the module’s manifest __manifest__.py.

  6. Then, push the changes.

    Tipp

    Stage the requirements.txt file by running git add requirements.txt.