commit
e9ca8b49ca
|
@ -0,0 +1 @@
|
||||||
|
from . import fields
|
|
@ -0,0 +1,13 @@
|
||||||
|
# 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": "13.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,
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
# 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
|
|
@ -0,0 +1 @@
|
||||||
|
* Akim Juillerat <akim.juillerat@camptocamp.com>
|
|
@ -0,0 +1 @@
|
||||||
|
This module adds a new Many2many custom field with a `create_table` attribute.
|
|
@ -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
|
||||||
|
|
||||||
|
<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…
Reference in New Issue