Writing and extending addons: a two-addon tutorial
Status: verified against the integration suite crates/erp-test-models/tests/two_addon_lifecycle.rs.
Implements: ADR 0001 (addon mechanism), ADR 0005 (workspace and crate map), ADR 0009 (addon platform runtime).
This tutorial walks through building, extending, installing, upgrading, and uninstalling two native ERPForAll addons:
test_addon_a(Base Addon): Declares a new modeltest_addon.book, provides XML seed records, defines form and list views, and implements a custom methoddescribe().test_addon_b(Extension Addon): Declares a dependency ontest_addon_a, extendstest_addon.bookwith new fields (isbn,rating), overridesdescribe()usingSuperchaining, and injects the new fields into the base form view via XPath.
Overview and architecture
Section titled “Overview and architecture”In ERPForAll, native addons are Rust crates that integrate into the unified ORM and view registries:
crates/erp-test-models/addons/├── test_addon_a/ # Base addon│ ├── Cargo.toml│ ├── manifest.toml│ ├── src/│ │ ├── lib.rs # erp_sdk::addon!(name = "test_addon_a")│ │ └── models.rs # #[model] struct Book│ ├── data/│ │ └── books.xml # Seed data records│ └── views/│ └── book_views.xml # List and form view architectures│└── test_addon_b/ # Extension addon ├── Cargo.toml # Depends on erp-addon-test_addon_a ├── manifest.toml # depends = ["base", "test_addon_a"] ├── src/ │ ├── lib.rs # erp_sdk::addon!(name = "test_addon_b", depends = ["test_addon_a"]) │ └── models.rs # #[extend(Book)] struct BookExtension └── views/ └── book_views.xml # XPath inheritance patching test_addon_a.view_book_formStep 1: Authoring the base addon
Section titled “Step 1: Authoring the base addon”The base addon declares the core business entity and its default views. The snippets follow the
fixtures in crates/erp-test-models/addons/test_addon_a and test_addon_b, abbreviated: the real
Cargo.toml files take their versions and lints from the workspace (.workspace = true), which needs
a Cargo workspace.
1. Crate configuration (Cargo.toml)
Section titled “1. Crate configuration (Cargo.toml)”[package]name = "erp-addon-test_addon_a"version = "1.0.0"edition = "2021"description = "Fixture addon test_addon_a: two-addon suite base"
[dependencies]erp-sdk = { workspace = true }2. Manifest (manifest.toml)
Section titled “2. Manifest (manifest.toml)”name = "Test Addon A"summary = "Fixture: defines test_addon.book (two-addon suite)"version = "1.0.0"depends = ["base"]data = [ "data/books.xml", "views/book_views.xml",]3. Addon registration (src/lib.rs)
Section titled “3. Addon registration (src/lib.rs)”//! Fixture addon `test_addon_a`: defines `test_addon.book`, views, data and `describe` method.
pub mod models;pub use models::*;
erp_sdk::addon!(name = "test_addon_a");4. Model definition (src/models.rs)
Section titled “4. Model definition (src/models.rs)”use erp_sdk::prelude::*;
/// `test_addon.book`.#[model(name = "test_addon.book", description = "Book")]pub struct Book { /// Book title. #[field(char, required = true)] pub name: Option<Arc<str>>, /// Author name. #[field(char)] pub author: Option<Arc<str>>, /// Publication year. #[field(integer)] pub published_year: i32,}
#[methods(Book)]impl Book { /// `describe()`: returns title and author. #[method] pub async fn describe(this: &RecordSet<Book>, tx: &mut Transaction) -> Result<String> { let name = this.get(tx, Book::NAME).await?; let author = this.get(tx, Book::AUTHOR).await?; Ok(format!( "{} by {}", name.as_deref().unwrap_or("?"), author.as_deref().unwrap_or("Anonymous") )) }}5. Seed data (data/books.xml)
Section titled “5. Seed data (data/books.xml)”<?xml version="1.0" encoding="utf-8"?><odoo> <record id="book_dune" model="test_addon.book"> <field name="name">Dune</field> <field name="author">Frank Herbert</field> <field name="published_year">1965</field> </record> <record id="book_foundation" model="test_addon.book"> <field name="name">Foundation</field> <field name="author">Isaac Asimov</field> <field name="published_year">1951</field> </record></odoo>6. Views (views/book_views.xml)
Section titled “6. Views (views/book_views.xml)”<?xml version="1.0" encoding="utf-8"?><odoo> <record id="view_book_tree" model="ir.ui.view"> <field name="name">test_addon.book.tree</field> <field name="model">test_addon.book</field> <field name="arch" type="xml"> <list string="Books"> <field name="name"/> <field name="author"/> <field name="published_year"/> </list> </field> </record> <record id="view_book_form" model="ir.ui.view"> <field name="name">test_addon.book.form</field> <field name="model">test_addon.book</field> <field name="arch" type="xml"> <form string="Book"> <sheet> <group> <field name="name"/> <field name="author"/> <field name="published_year"/> </group> </sheet> </form> </field> </record></odoo>Step 2: Authoring the extension addon
Section titled “Step 2: Authoring the extension addon”The extension addon enhances test_addon.book by adding fields, overriding behavior, and patching views.
1. Crate configuration (Cargo.toml)
Section titled “1. Crate configuration (Cargo.toml)”[package]name = "erp-addon-test_addon_b"version = "1.0.0"edition = "2021"description = "Fixture addon test_addon_b: extends test_addon_a"
[dependencies]erp-sdk = { workspace = true }erp-addon-test_addon_a = { workspace = true }2. Manifest (manifest.toml)
Section titled “2. Manifest (manifest.toml)”name = "Test Addon B"summary = "Fixture: extends test_addon.book with ISBN, rating and inherited view"version = "1.0.0"depends = ["base", "test_addon_a"]data = [ "views/book_views.xml",]3. Addon registration (src/lib.rs)
Section titled “3. Addon registration (src/lib.rs)”//! Fixture addon `test_addon_b`: extends `test_addon.book`.
pub mod models;pub use models::*;
erp_sdk::addon!(name = "test_addon_b", depends = ["test_addon_a"]);4. Model extension and method override (src/models.rs)
Section titled “4. Model extension and method override (src/models.rs)”use erp_addon_test_addon_a::Book;use erp_sdk::prelude::*;
/// `test_addon_b`'s extension of `test_addon.book`.#[extend(Book)]pub struct BookExtension { /// ISBN. #[field(char, string = "ISBN")] pub isbn: Option<Arc<str>>, /// Rating. #[field(float, string = "Rating")] pub rating: f64,}
#[methods(Book)]impl BookExtension { /// Appends the rating to the base description using `Super` chaining. #[method(override)] pub async fn describe( this: &RecordSet<Book>, tx: &mut Transaction, sup: Super, ) -> Result<String> { let base_desc = sup.call(tx, this.as_records(), ()).await?; let rating = this.get(tx, BookExtension::RATING).await?; Ok(format!("{base_desc} (Rating: {rating:.1})")) }}5. Inherited view patch (views/book_views.xml)
Section titled “5. Inherited view patch (views/book_views.xml)”<?xml version="1.0" encoding="utf-8"?><odoo> <record id="view_book_form_inherit" model="ir.ui.view"> <field name="name">test_addon.book.form.inherit</field> <field name="model">test_addon.book</field> <field name="inherit_id" ref="test_addon_a.view_book_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='published_year']" position="after"> <field name="isbn"/> <field name="rating"/> </xpath> </field> </record></odoo>Step 3: Installing both addons
Section titled “Step 3: Installing both addons”Command-line installation
Section titled “Command-line installation”Install both modules into your database with one command. --addons-path defaults to ./addons;
the two fixture addons live in crates/erp-test-models/addons, so name that directory (or use your
own addons directory):
erp module install -d mydb --addons-path crates/erp-test-models/addons test_addon_a test_addon_bThe dependency resolver orders the modules by their depends, so test_addon_a is loaded before
test_addon_b.
Programmatic installation
Section titled “Programmatic installation”The lifecycle test (crates/erp-test-models/tests/two_addon_lifecycle.rs) builds the module system
from a database pool and the addon index, links the native code of the three addons, initializes the
database and installs one addon at a time (abbreviated; the test also binds each fixture’s artifact
identity with .native_artifact_identities(...)):
let system = ModuleSystem::builder(db.pool().clone(), index) .code([ &erp_addon_base::ADDON, &erp_addon_test_addon_a::ADDON, &erp_addon_test_addon_b::ADDON, ]) .build()?;
let init = system .initialize(InitOptions::new().with_skip_auto_install(true)) .await?;
// `su` gives a superuser environment on the given ORM; each install returns the updated ORM.let outcome_a = system .button_immediate_install(&su(&init.orm), &[n("test_addon_a")]) .await?;let outcome_b = system .button_immediate_install(&su(&outcome_a.orm), &[n("test_addon_b")]) .await?;Verification of installed state
Section titled “Verification of installed state”-
Database Schema: The table
test_addon_bookis created in PostgreSQL with both base and extended columns:- Base columns:
id,name,author,published_year - Extension columns:
isbn,rating - System audit columns:
create_uid,create_date,write_uid,write_date
- Base columns:
-
Data Records: Seed records from
data/books.xmlare inserted, and their external identifiers are recorded inir_model_data:test_addon_a.book_dunetest_addon_a.book_foundation
-
Combined View Evaluation: When the web client requests the form view via
get_views, the view combination engine applies the XPath patch fromtest_addon_b.view_book_form_inherit:<form string="Book"><sheet><group><field name="name"/><field name="author"/><field name="published_year"/><field name="isbn"/><field name="rating"/></group></sheet></form> -
Method Override Chaining: Calling
book.describe(&mut tx)executesBookExtension::describe, which invokessup.call(...)to get the base description before formatting the rating:Dune by Frank Herbert (Rating: 4.8)(after the test sets Dune’s rating to 4.8; the override formats the rating with one decimal).
Step 4: Upgrading the base addon
Section titled “Step 4: Upgrading the base addon”When upgrading an addon (e.g. after adding fields or updating seed data), run:
erp module upgrade -d mydb test_addon_aThe upgrade is one PostgreSQL transaction (ADR 0009 §2). The lifecycle test checks that both
addons stay installed and that Dune keeps its rating across the upgrade. What an upgrade does with
migrations, noupdate records and removed external ids is described in modules and
data files.
Step 5: Uninstalling and dependency constraints
Section titled “Step 5: Uninstalling and dependency constraints”Dependency refusal check
Section titled “Dependency refusal check”Uninstalling test_addon_a while test_addon_b is installed also uninstalls test_addon_b, as
in Odoo: a live run cascades to the installed dependents and reports them. To see the effect first,
use --dry-run, which plans without database state and refuses when an available addon depends on
the one you named:
erp module uninstall -d mydb --addons-path crates/erp-test-models/addons --dry-run test_addon_acannot uninstall module 'test_addon_a' because module 'test_addon_b' depends on it (conservative plan-only check on available addons; a live run cascades to the installed dependents)Clean uninstallation order
Section titled “Clean uninstallation order”To remove the functionality cleanly:
-
Uninstall the dependent extension first:
Terminal window erp module uninstall -d mydb test_addon_b- Extended columns
isbnandratingare safely dropped fromtest_addon_book. - The inherited view patch
view_book_form_inheritis deleted, restoring the original form view. ir_model_datarows fortest_addon_bare purged.
- Extended columns
-
Uninstall the base module:
Terminal window erp module uninstall -d mydb test_addon_a- The table
test_addon_bookis dropped. - The base views, actions, menus, and external ID entries are purged from the database.
- Module state in
ir_module_moduletransitions touninstalled.
- The table
Key takeaways and best practices
Section titled “Key takeaways and best practices”- One transaction per operation: an install, upgrade or uninstall is one PostgreSQL transaction (ADR 0009 §2); when anything fails, nothing of the operation is kept. See modules.
- Strongly Typed Extension: Extensions use Rust’s type system with
#[extend(Model)]and explicitSuperchaining instead of loose string dispatch. - Fail loudly: an unknown module is refused with an error that names it.