Skip to Content
Menu
This question has been flagged
2 Replies
22316 Views

I want to create a many2many relation in res.partner (self related). This relationship needs to have attributes.

For example: PartnerA is 'manager' of PartnerB. PartnerB is 'owner' of PartnerB1. PartnerC is 'business agent' of PartnerA. ....

The type of relation (enclosed by '') will be a new model partner_relations.

How can I implement this?

Avatar
Discard
Best Answer

To create a self-related Many2many relation, the following parameters must be used: relation, column1 and column2. The value given to the former parameter will become the table name in the database, while the values given to column1 and column2 will become field names for the afore mentioned table so, in order to add attributes, we simply declare a new model which uses exactly the same table and fields, thus tricking the Odoo's ORM.

from openerp import api, fields, models

class MainModel(models.Model):
    _name = 'mymodule.mainmodel'
    # See https://www.odoo.com/documentation/10.0/reference/orm.html#relational-fields 
    related_ids = fields.Many2many('mymodule.mainmodel', relation='mymodule_mainmodel_rel', column1='left', column2='right', string='Related instances')
    # ... more field definitions

class MainmodelRelations(models.Model):
    _name = 'mymodule.mainmodel.rel'
    left = fields.Integer(string='Left', required=True)
    right = fields.Integer(string='Right', required=True)
    # ... more field definitions:
relationship = fields.Selection(string='Left to Right Relationship', selection='_get_relationship', required=True)
    def _get_relationship(self):
        return [
            ('01','relationship1'),
            ('02','relationship2'),
            ('03','relationship3')#,
      # ... and so on
        ]
# In case the relationship is not reflexive, otherwise this can be omitted
    @api.one
    @api.constrains('left', 'right')
    def _check_description(self):
        if self.left == self.right:
            raise ValidationError("A MainModel instance cannot be related to itself.")

Note: This is a redacted code taken from one that worked for me with Odoo 10 but I'm confident it works with earlier versions.


Avatar
Discard
Best Answer

#file: res.partner.py

from osv import osv
from osv import fields

class res_partner_rel(osv.osv):
        _name = 'res.partner.rel'
        _columns = {
                   'partner_left_id' : fields.many2one('res.partner'),
                   'partner_right_id': fields.many2one('res.partner','Relationed Partner'),
                   'property_left2right': fields.char('Relation',size=32),
                   }
class res_partner(osv.osv):
        _name = 'res.partner'
        _inherit = 'res.partner'
        _columns = {
                    'm2m_right2left' : fields.many2many('res.partner','res_partner_rel','partner_right_id','partner_left_id'),
                    'm2m_left2right' : fields.many2many('res.partner','res_partner_rel','partner_left_id','partner_right_id'),
                    'o2m_left_ids': fields.one2many('res.partner.rel','partner_left_id'),
                    }

<

#file: partner_view.xml

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <record id="base_view_partner_form" model="ir.ui.view">
            <field name="name">openjobs.res.partner.form</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form" />
            <field name="arch" type="xml">
                <data>
                    <notebook position="inside">
                        <page string="Link Relations">
                            <field name="m2m_left2right" widget="many2many" />
                        </page>
                        <page string="Relations">
                            <field name="o2m_left_ids" widget="one2many" >
                                <tree>
                                    <field name='property_left2right'/>
                                    <field name='partner_right_id'/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                 </data>
            </field>
        </record>
    </data>
</openerp>
Avatar
Discard
Related Posts Replies Views Activity
2
Jun 17
5608
2
Mar 15
10800
3
Jun 18
13996
0
Oct 16
2054
1
Feb 24
839