From f52cbe0fdda5825c9f6eb28fde360515907cfcef Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Thu, 5 Dec 2019 15:12:14 +0100 Subject: [PATCH 1/2] 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..dffa98a97 --- /dev/null +++ b/base_m2m_custom_field/__init__.py @@ -0,0 +1 @@ +from . import fields \ No newline at end of file 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..04132fc68 --- /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, + ) + my_model_b_id = fields.Many2one( + 'my.model.b', + required=True, + index=True, + ) + + +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 f6e029c717cc6c9c1cfe83c71a2f53d6690fee84 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Wed, 8 Jan 2020 16:23:56 +0100 Subject: [PATCH 2/2] fixup! Add module base_m2m_custom_field --- base_m2m_custom_field/__init__.py | 2 +- base_m2m_custom_field/readme/USAGE.rst | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/base_m2m_custom_field/__init__.py b/base_m2m_custom_field/__init__.py index dffa98a97..735443893 100644 --- a/base_m2m_custom_field/__init__.py +++ b/base_m2m_custom_field/__init__.py @@ -1 +1 @@ -from . import fields \ No newline at end of file +from . import fields diff --git a/base_m2m_custom_field/readme/USAGE.rst b/base_m2m_custom_field/readme/USAGE.rst index 04132fc68..834fa397d 100644 --- a/base_m2m_custom_field/readme/USAGE.rst +++ b/base_m2m_custom_field/readme/USAGE.rst @@ -38,12 +38,12 @@ Let's consider following models: my_model_a_id = fields.Many2one( 'my.model.a', required=True, - index=True, + index=True, # Index is mandatory here ) my_model_b_id = fields.Many2one( 'my.model.b', required=True, - index=True, + index=True, # Index is mandatory here )