Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

many2many relation with attributes

Subscribe

Get notified when there's activity on this post

This question has been flagged
relationshipmany2manyattribute
2 Replies
25148 Views
Avatar
Martin Varela

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?

0
Avatar
Discard
Avatar
Adan Cortes Medina
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.


1
Avatar
Discard
Avatar
Evelb Tecnicas Y Sistemas Sl.
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>
0
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
How to get Value for Relation table in odoo 10 Solved
relationship many2many odoo10
Avatar
Avatar
2
Jun 17
7117
Why my bidirectional many2many is not showing data?
inheritance relationship many2many
Avatar
Avatar
2
Mar 15
12644
Many2many relation on the same model does not work Solved
fields relationship many2many model
Avatar
Avatar
3
Jun 18
15784
Allowing groups to only edit relationship fields in a model
security relationship many2many groups
Avatar
0
Oct 16
3356
ODOO V16 Associate Parts to a Finished Good
many2many
Avatar
Avatar
1
Feb 24
2898
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now