Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 254 additions & 0 deletions base_autocomplete_template/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
===========================
Base Autocomplete Templates
===========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:36224729669f87589f45a941303e973d1a2404ebb481dda00f67b66dc568aad6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/17.0/base_autocomplete_template
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-17-0/server-tools-17-0-base_autocomplete_template
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module provides a **generic**, reusable mechanism to **save** and
**apply** named templates that autocomplete any Odoo wizard/form using a
**JSON payload** stored in a template record.

It is designed to be **framework-like**: other modules can inherit the
mixin and add a few buttons in XML.

--------------

What you get
------------

1) Template model
~~~~~~~~~~~~~~~~~

``data.autocomplete.template``

Each template stores:

- ``name``: template name
- ``model_id``: the target Odoo model (``ir.model``) the template
applies to
- ``company_id``: company scoping (global or per company)
- ``user_id``: optional owner (personal templates)
- ``values_json``: a JSON dictionary holding serialized default values

2) Mixin
~~~~~~~~

``data.template.mixin``

Add it to any wizard/model to get:

- ``template_id`` field (select a template)
- actions (for buttons):

- ``action_open_create_template_wizard()``
- ``action_update_current_template()``
- ``action_apply_template(overwrite=True)``

3) Create Template Wizard
~~~~~~~~~~~~~~~~~~~~~~~~~

``data.wizard.template.create``

This popup asks for a **name** (and personal/global scope) and creates
the template using the current wizard/model values.

JSON serialization
~~~~~~~~~~~~~~~~~~

The mixin serializes current values into a JSON-safe dict:

- ``many2one`` -> the record ``id`` (or ``false``)
- ``many2many`` -> a list of record ids
- basic fields
(char/int/float/bool/date/datetime/selection/text/monetary) -> stored
as-is
- ``one2many`` is ignored
- computed fields without inverse and readonly fields are ignored (by
default)

Applying templates safely with onchanges
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Applying a template uses a **phased write**:

1. **Drivers**: fields returned by ``_template_driver_fields()`` are
written first
2. **Rest**: everything else (excluding protected fields)
3. **Protected**: fields returned by ``_template_protected_fields()``
are written last

This solves common wizard issues where changing a driver field triggers
an onchange that clears other fields (e.g. changing ``plan_id`` clears
``account_ids``).

While applying, ``apply_template=True`` is present in the context so you
can skip destructive onchanges in your own code.

**Table of contents**

.. contents::
:local:

Usage
=====

1) Inherit the mixin in Python
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

from odoo import models, api

class MyWizard(models.TransientModel):
_inherit = ["my.wizard.model", "data.template.mixin"]

# Optional: define drivers/protected fields to survive onchanges
def _template_driver_fields(self):
return {"plan_id"}

def _template_protected_fields(self):
return {"account_ids", "show_months"}

@api.onchange("plan_id")
def _onchange_plan_id(self):
# Skip destructive behavior when applying templates
if self.env.context.get("apply_template"):
return
return super()._onchange_plan_id()

..

Note: This module only provides the base.

2) Add UI in XML (template selector + buttons)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Insert this into your wizard form view (adapt ``inherit_id`` and the
``xpath``):

.. code:: xml

<separator string="Templates"/>
<group col="4">
<field name="template_id" options="{'no_create': True}"/>

<button name="action_apply_template"
type="object"
string="Apply"
class="btn btn-secondary"
icon="fa-check"
invisible="not template_id"/>

<button name="action_open_create_template_wizard"
type="object"
string="Save as template"
class="btn btn-secondary"
icon="fa-save"/>

<button name="action_update_current_template"
type="object"
string="Update template"
class="btn btn-secondary"
icon="fa-refresh"
invisible="not template_id"/>
</group>

3) Filtering templates further (optional)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If the same wizard model can represent different "types" of reports
(e.g. different ``report_id``), you can further restrict the templates
shown by overriding:

.. code:: python

def _template_domain_extra(self):
return [("some_field", "=", self.some_field.id)]

--------------

Notes / Tips
~~~~~~~~~~~~

- If you want templates shared across companies, allow
``company_id = False`` templates (already supported by the domain).
- If you want only personal templates, set ``user_id`` on every
template and adjust the domain.
- If some fields should never be stored (tokens, volatile flags, etc.),
override ``_template_skip_fields()``.

--------------

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_autocomplete_template%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* APSL/Nagarro

Contributors
------------

- `APSL-Nagarro <https://apsl.tech>`__:

- Bernat Obrador <bobrador@apsl.net>

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

.. |maintainer-BernatObrador| image:: https://github.com/BernatObrador.png?size=40px
:target: https://github.com/BernatObrador
:alt: BernatObrador

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-BernatObrador|

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/17.0/base_autocomplete_template>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions base_autocomplete_template/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizards
19 changes: 19 additions & 0 deletions base_autocomplete_template/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2026 (APSL-Nagarro) Bernat Obrador
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Base Autocomplete Templates",
"summary": "Generic JSON-based templates to autocomplete any wizard/form.",
"version": "17.0.1.0.0",
"author": "APSL/Nagarro, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/server-tools",
"depends": ["base"],
"data": [
"security/ir.model.access.csv",
"views/data_autocomplete_template_views.xml",
"wizards/data_autocomplete_create_wizard.xml",
],
"maintainers": ["BernatObrador"],
"installable": True,
"application": False,
}
Loading