diff --git a/base_m2m_custom_field/__init__.py b/base_m2m_custom_field/__init__.py new file mode 100644 index 000000000..735443893 --- /dev/null +++ b/base_m2m_custom_field/__init__.py @@ -0,0 +1 @@ +from . import fields diff --git a/base_m2m_custom_field/__manifest__.py b/base_m2m_custom_field/__manifest__.py new file mode 100644 index 000000000..76ae9eb5b --- /dev/null +++ b/base_m2m_custom_field/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +{ + "name": "Base Many2many Custom Field", + "summary": "Customizations of Many2many", + "version": "12.0.1.0.0", + "category": "Technical Settings", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "website": "https://github.com/OCA/server-tools", + "depends": [ + "base", + ], + "installable": True, +} diff --git a/base_m2m_custom_field/fields.py b/base_m2m_custom_field/fields.py new file mode 100644 index 000000000..6136f37fa --- /dev/null +++ b/base_m2m_custom_field/fields.py @@ -0,0 +1,23 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo import fields + + +class Many2manyCustom(fields.Many2many): + """ Many2manyCustom field is intended to customize Many2many properties. + + :param create_table: defines if the relational table must be created + at the initialization of the field (boolean) + """ + + _slots = { + 'create_table': True + } + + def update_db(self, model, columns): + if not self.create_table: + return + return super().update_db(model, columns) + + +fields.Many2manyCustom = Many2manyCustom diff --git a/base_m2m_custom_field/readme/CONTRIBUTORS.rst b/base_m2m_custom_field/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..e31e2f0c4 --- /dev/null +++ b/base_m2m_custom_field/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Akim Juillerat diff --git a/base_m2m_custom_field/readme/DESCRIPTION.rst b/base_m2m_custom_field/readme/DESCRIPTION.rst new file mode 100644 index 000000000..0a88d0367 --- /dev/null +++ b/base_m2m_custom_field/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module adds a new Many2many custom field with a `create_table` attribute. diff --git a/base_m2m_custom_field/readme/USAGE.rst b/base_m2m_custom_field/readme/USAGE.rst new file mode 100644 index 000000000..834fa397d --- /dev/null +++ b/base_m2m_custom_field/readme/USAGE.rst @@ -0,0 +1,65 @@ +Many2manyCustom field is useful when a direct access to the relational table +is needed, for example to be editable in a dedicated tree view. + +Let's consider following models: + +.. code-block:: python + + class MyModelA(models.Model): + + _name = 'my.model.a' + + my_model_b_ids = fields.Many2manyCustom( + 'my.model.b', + 'my_model_a_b_rel', + 'my_model_a_id', + 'my_model_b_id', + create_table=False, + ) + + + class MyModelB(models.Model): + + _name = 'my.model.b' + + my_model_a_ids = fields.Many2manyCustom( + 'my.model.a', + 'my_model_a_b_rel', + 'my_model_b_id', + 'my_model_a_id', + create_table=False, + ) + + + class MyModelABRel(models.Model): + + _name = 'my.model.a.b.rel' + + my_model_a_id = fields.Many2one( + 'my.model.a', + required=True, + index=True, # Index is mandatory here + ) + my_model_b_id = fields.Many2one( + 'my.model.b', + required=True, + index=True, # Index is mandatory here + ) + + +By setting `create_table=False` on the Many2manyCustom field, and using the +relational table name, as `_name` for the relational model, we're able to +define a dedicated tree view for `my.model.a.b.rel`. + +.. code-block:: xml + + + my.model.a.b.rel.tree.view + my.model.a.b.rel + + + + + + +