Skip to content
ERPForAll DocsDevelopment

    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:

    1. test_addon_a (Base Addon): Declares a new model test_addon.book, provides XML seed records, defines form and list views, and implements a custom method describe().
    2. test_addon_b (Extension Addon): Declares a dependency on test_addon_a, extends test_addon.book with new fields (isbn, rating), overrides describe() using Super chaining, and injects the new fields into the base form view via XPath.

    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_form

    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.

    [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 }
    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",
    ]
    //! 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");
    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")
    ))
    }
    }
    <?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>
    <?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>

    The extension addon enhances test_addon.book by adding fields, overriding behavior, and patching views.

    [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 }
    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",
    ]
    //! 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>

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

    Terminal window
    erp module install -d mydb --addons-path crates/erp-test-models/addons test_addon_a test_addon_b

    The dependency resolver orders the modules by their depends, so test_addon_a is loaded before test_addon_b.

    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?;
    1. Database Schema: The table test_addon_book is 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
    2. Data Records: Seed records from data/books.xml are inserted, and their external identifiers are recorded in ir_model_data:

      • test_addon_a.book_dune
      • test_addon_a.book_foundation
    3. Combined View Evaluation: When the web client requests the form view via get_views, the view combination engine applies the XPath patch from test_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>
    4. Method Override Chaining: Calling book.describe(&mut tx) executes BookExtension::describe, which invokes sup.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).


    When upgrading an addon (e.g. after adding fields or updating seed data), run:

    Terminal window
    erp module upgrade -d mydb test_addon_a

    The 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”

    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:

    Terminal window
    erp module uninstall -d mydb --addons-path crates/erp-test-models/addons --dry-run test_addon_a
    cannot 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)

    To remove the functionality cleanly:

    1. Uninstall the dependent extension first:

      Terminal window
      erp module uninstall -d mydb test_addon_b
      • Extended columns isbn and rating are safely dropped from test_addon_book.
      • The inherited view patch view_book_form_inherit is deleted, restoring the original form view.
      • ir_model_data rows for test_addon_b are purged.
    2. Uninstall the base module:

      Terminal window
      erp module uninstall -d mydb test_addon_a
      • The table test_addon_book is dropped.
      • The base views, actions, menus, and external ID entries are purged from the database.
      • Module state in ir_module_module transitions to uninstalled.

    • 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 explicit Super chaining instead of loose string dispatch.
    • Fail loudly: an unknown module is refused with an error that names it.