Odoo Help


many2many relation with attributes

Martin Varela
on 11/27/13, 8:38 AM 3,803 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?

Adan Cortes Medina
On 6/22/17, 10:00 PM

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 [
      # ... and so on
# In case the relationship is not reflexive, otherwise this can be omitted
    @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.

raul paz
On 4/24/14, 2:35 PM

#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"?>
        <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">
                    <notebook position="inside">
                        <page string="Link Relations">
                            <field name="m2m_left2right" widget="many2many" />
                        <page string="Relations">
                            <field name="o2m_left_ids" widget="one2many" >
                                    <field name='property_left2right'/>
                                    <field name='partner_right_id'/>

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 11/27/13, 8:38 AM
Seen: 3803 times
Last updated: 6/22/17, 10:10 PM