From f2618b5d1900983d00fc8a6382ce346efc7aacf8 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Thu, 5 Dec 2019 15:12:14 +0100 Subject: [PATCH 1/3] Add module base_m2m_custom_field --- base_m2m_custom_field/__init__.py | 1 + base_m2m_custom_field/__manifest__.py | 15 +++++ base_m2m_custom_field/fields.py | 23 +++++++ base_m2m_custom_field/readme/CONTRIBUTORS.rst | 1 + base_m2m_custom_field/readme/DESCRIPTION.rst | 1 + base_m2m_custom_field/readme/USAGE.rst | 65 +++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 base_m2m_custom_field/__init__.py create mode 100644 base_m2m_custom_field/__manifest__.py create mode 100644 base_m2m_custom_field/fields.py create mode 100644 base_m2m_custom_field/readme/CONTRIBUTORS.rst create mode 100644 base_m2m_custom_field/readme/DESCRIPTION.rst create mode 100644 base_m2m_custom_field/readme/USAGE.rst 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 + + + + + + + From 981adfdf7e2c17f44e757aa30b987e4534e03841 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Wed, 8 Jan 2020 16:26:27 +0100 Subject: [PATCH 2/3] [IMP] base_m2m_custom_field: black, isort --- base_m2m_custom_field/__manifest__.py | 4 +--- base_m2m_custom_field/fields.py | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/base_m2m_custom_field/__manifest__.py b/base_m2m_custom_field/__manifest__.py index 76ae9eb5b..7d72be6d1 100644 --- a/base_m2m_custom_field/__manifest__.py +++ b/base_m2m_custom_field/__manifest__.py @@ -8,8 +8,6 @@ "author": "Camptocamp, Odoo Community Association (OCA)", "license": "AGPL-3", "website": "https://github.com/OCA/server-tools", - "depends": [ - "base", - ], + "depends": ["base"], "installable": True, } diff --git a/base_m2m_custom_field/fields.py b/base_m2m_custom_field/fields.py index 6136f37fa..65729bd45 100644 --- a/base_m2m_custom_field/fields.py +++ b/base_m2m_custom_field/fields.py @@ -10,9 +10,7 @@ class Many2manyCustom(fields.Many2many): at the initialization of the field (boolean) """ - _slots = { - 'create_table': True - } + _slots = {"create_table": True} def update_db(self, model, columns): if not self.create_table: From 4d3af5a6017d1ae9ceeedbf48232ce7a0c900511 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Wed, 8 Jan 2020 16:29:29 +0100 Subject: [PATCH 3/3] [MIG] base_m2m_custom_field: Migration to 13.0 --- base_m2m_custom_field/__manifest__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base_m2m_custom_field/__manifest__.py b/base_m2m_custom_field/__manifest__.py index 7d72be6d1..8fb5e575d 100644 --- a/base_m2m_custom_field/__manifest__.py +++ b/base_m2m_custom_field/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Base Many2many Custom Field", "summary": "Customizations of Many2many", - "version": "12.0.1.0.0", + "version": "13.0.1.0.0", "category": "Technical Settings", "author": "Camptocamp, Odoo Community Association (OCA)", "license": "AGPL-3",