Browse Source

Add module base_m2m_custom_field

12.0-mig-module_prototyper_last
Akim Juillerat 5 years ago
parent
commit
f52cbe0fdd
  1. 1
      base_m2m_custom_field/__init__.py
  2. 15
      base_m2m_custom_field/__manifest__.py
  3. 23
      base_m2m_custom_field/fields.py
  4. 1
      base_m2m_custom_field/readme/CONTRIBUTORS.rst
  5. 1
      base_m2m_custom_field/readme/DESCRIPTION.rst
  6. 65
      base_m2m_custom_field/readme/USAGE.rst

1
base_m2m_custom_field/__init__.py

@ -0,0 +1 @@
from . import fields

15
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,
}

23
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

1
base_m2m_custom_field/readme/CONTRIBUTORS.rst

@ -0,0 +1 @@
* Akim Juillerat <akim.juillerat@camptocamp.com>

1
base_m2m_custom_field/readme/DESCRIPTION.rst

@ -0,0 +1 @@
This module adds a new Many2many custom field with a `create_table` attribute.

65
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
<record id="my_model_a_b_rel_tree_view" model="ir.ui.view">
<field name="name">my.model.a.b.rel.tree.view</field>
<field name="model">my.model.a.b.rel</field>
<field name="arch" type="xml">
<tree editable="top">
<field name="my_model_a_id" />
<field name="my_model_b_id" />
</tree>
</field>
</record>
Loading…
Cancel
Save