Blog

Odoo custom module: build and test it on Odoo 20

By The Ofleet team8 min read

Contents

An Odoo custom module needs Python imports, a manifest, database access permissions and a way for users to reach its records. On Odoo 20, that means ir.access.csv: copying an older tutorial's ir.model.access.csv gives you the wrong security model. This walkthrough builds a small asset register and checks it with a regular internal user, not just an administrator. The result runs on Community 20; Odoo Online cannot host this server-side module.

1. Start with a module, even when you use _inherit#

Create a module to package your changes; _inherit is a choice inside that module, not an alternative to having one. Adding a field to res.partner normally extends that existing model, whereas this walkthrough deliberately creates lab.asset so we can inspect the model, permissions and menu without modifying contacts. Contacts stay untouched.

The commands below assume an existing Odoo 20 source installation, its Python environment activated, PostgreSQL available and a working lab.conf. Its database role must be allowed to create the disposable database lab20. No production service should use that database or configuration. For a container-based environment, use the Odoo Docker setup and restore walkthrough, then adapt paths and the executable rather than mixing host and container paths.

From your Odoo source checkout, with a new, empty custom-addons directory, run:

mkdir custom-addons
./odoo-bin scaffold lab_asset custom-addons

This creates files, not database tables. Replace the generated files with the contents shown below. The root import must load only models; generated example controllers and demo files are not used. Keep these eight files, with paths relative to custom-addons/lab_asset:

__init__.py
__manifest__.py
models/__init__.py
models/asset.py
security/ir.access.csv
views/asset_views.xml
tests/__init__.py
tests/test_asset.py

I would keep this first module boring. An asset with a name is enough to expose a missing import or an access mistake; adding accounting logic now would only make those failures harder to locate.

2. Make the manifest load exactly what you wrote#

The manifest declares dependencies and the data files Odoo loads; it does not import your Python model. Those are separate jobs. The complete __manifest__.py below uses the fields documented in the Odoo 20 module reference (nouvel onglet), with the security file first in the data list and no demonstration data to load:

{
    "name": "Lab assets",
    "version": "20.0.1.0.0",
    "depends": ["base"],
    "data": ["security/ir.access.csv", "views/asset_views.xml"],
    "application": True,
    "license": "LGPL-3",
}

20.0.1.0.0 is this example's release convention, not proof that code written for an earlier release works on 20. The base dependency supplies the framework objects we use. If you later extend a sales model, declare the module that owns it instead of relying on something already installed on your machine.

Put from . import models in the root __init__.py and from . import asset in models/__init__.py. Then save this as models/asset.py:

from odoo import fields, models
 
 
class LabAsset(models.Model):
    _name = "lab.asset"
    _description = "Lab asset"
 
    name = fields.Char(required=True)

Odoo registers lab.asset when the Python file is imported; installation creates its database structure. A Python file sitting in the directory but missing from the import chain does neither. When a view reports an unknown model, I check those two __init__.py files before touching the XML.

3. Use Odoo 20 access permissions, not an older CSV#

Odoo 20 uses ir.access with an operation field and an optional record domain. The version 20 security reference (nouvel onglet) describes this model, including an important distinction: a permission has a group; an empty group defines a restriction applied to every user. Leaving the group blank is not a shortcut for granting everyone access.

Save this as security/ir.access.csv:

id,name,model_id/id,group_id/id,operation,domain
access_lab_asset_user,Lab asset internal user,model_lab_asset,base.group_user,crud,

The model reference points to the generated external identifier model_lab_asset. base.group_user selects internal users, and crud grants create, read, update and delete. The empty domain imposes no record filter on this permission. This is intentionally broad: every internal user can edit every lab asset, across this example's records.

That is acceptable for a disposable teaching register, not a company isolation policy. Before storing restricted or company-specific equipment, define the ownership and company behavior, then test the corresponding domains and users. Hiding a menu would not protect those records through the ORM or an API, even if a user could no longer find the screen in the application. Permissions belong on the model.

4. Connect list and form views to an action#

An action opens the model, and a menu opens the action. Save the following as views/asset_views.xml; these Odoo 20 views use list, not the older tree spelling. Their declarations follow the model → views → action → menu flow of the official server framework tutorial (nouvel onglet).

<odoo>
    <record id="asset_list" model="ir.ui.view">
        <field name="name">lab.asset.list</field>
        <field name="model">lab.asset</field>
        <field name="arch" type="xml">
            <list><field name="name"/></list>
        </field>
    </record>
    <record id="asset_form" model="ir.ui.view">
        <field name="name">lab.asset.form</field>
        <field name="model">lab.asset</field>
        <field name="arch" type="xml">
            <form><sheet><group><field name="name"/></group></sheet></form>
        </field>
    </record>
    <record id="asset_action" model="ir.actions.act_window">
        <field name="name">Lab assets</field>
        <field name="res_model">lab.asset</field>
        <field name="view_mode">list,form</field>
    </record>
    <menuitem id="asset_root" name="Lab" sequence="10"/>
    <menuitem id="asset_menu" name="Assets" parent="asset_root" action="asset_action"/>
</odoo>

The manifest loads the permission file before the views. Inside the XML, the action exists before the menu references it. External identifiers such as asset_action belong to the module's namespace: keep them stable when updating an installed module. Renaming an identifier is not the same thing as changing the text a user sees.

Diagram: module files load a model, access permissions and views into a disposable database, followed by non-admin tests

5. Test the Odoo custom module without administrator privileges#

A useful first test proves that an internal user can manage a record and a portal user cannot create one. Put from . import test_asset in tests/__init__.py, and the following in tests/test_asset.py. Tests are discovered through that package; don't import it from the module's root.

from odoo.exceptions import AccessError
from odoo.tests import TransactionCase, tagged
from odoo.tests.common import new_test_user
 
 
@tagged("post_install", "-at_install")
class TestAsset(TransactionCase):
    def test_internal_user_crud(self):
        user = new_test_user(self.env, login="lab_user")
        assets = self.env["lab.asset"].with_user(user)
        asset = assets.create({"name": "Probe"})
        self.assertEqual(asset.name, "Probe")
        asset.write({"name": "Checked probe"})
        self.assertEqual(asset.name, "Checked probe")
        asset.unlink()
        self.assertFalse(asset.exists())
 
    def test_portal_user_cannot_create(self):
        user = new_test_user(self.env, login="lab_portal", groups="base.group_portal")
        with self.assertRaises(AccessError):
            self.env["lab.asset"].with_user(user).create({"name": "Forbidden"})

with_user is doing the work here. Without it, a test running as superuser could approve a module whose ordinary users cannot create anything, leaving you with a green test suite and an unusable asset register for the people it was written for. TransactionCase rolls back test changes; the testing reference (nouvel onglet) explains discovery and tags.

From the same checkout, replace /ABS/custom-addons with your new directory's absolute path. This command initializes lab20, installs the module and runs its tests; it modifies that database, not just the files:

./odoo-bin -c lab.conf -d lab20 \
  --addons-path=addons,/ABS/custom-addons \
  -i lab_asset --without-demo \
  --test-tags=/lab_asset --stop-after-init \
  --workers=0 --max-cron-threads=0 --no-http

--test-tags enables tests; adding --test-enable is unnecessary. Check both the exit status and the log summary: zero executed tests is not a pass. Find both TestAsset methods with no failures; the initial installation may also run framework tests. Start Odoo separately against lab20 through private access to check the Lab → Assets screens. ORM tests don't click buttons.

6. Ship a revision, then update the test database#

Deploy the same Git revision and dependencies before updating a second database. Keep credentials and lab.conf out of the repository. This module needs no additional Python package; a future external_dependencies declaration checks availability but does not install packages for you. Record Python and Linux dependencies in the environment build as well.

Changed file What must happen on the test instance
Python model Reload the Python code; update the module if fields or schema changed
XML view or permission CSV Deploy the file and update the module in the target database
Manifest dependency Make the dependency available, then update and rerun tests

Method: Odoo 20 Community, checked November 12, 2026 against the module and testing references above and the CLI below. This table covers this server-side example, not JavaScript assets or major-version migrations.

With other Odoo processes stopped on the test database, rerun the installation command with -u lab_asset instead of -i lab_asset. The Odoo 20 CLI reference (nouvel onglet) documents both switches. Updating changes database state; restarting alone does not reload your XML or permission data.

For production, take a matching database and filestore backup, preserve the old code revision and rehearse the update first. An old commit cannot undo a schema or data change. This is where the choice of Odoo hosting becomes concrete: Odoo.sh supports a Git-based module workflow, while Odoo Online excludes custom modules (nouvel onglet). If standard Online already covers the need, keeping it avoids maintaining this code.

For a separate place to run your repository and test instance, you can try Ofleet. I would first reproduce these two permission tests there, then add the next field. A new environment should not change who can touch your records.

Share this article