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

How to use many2many between companies and contacts?

Subscribe

Get notified when there's activity on this post

This question has been flagged
contactscompaniesres.partnermany2manyodoo10
1 Reply
7528 Views
Avatar
wizardz

I'm trying to use a many2many relation between companies and contacts.

Is there a way to use parent_id as many2many or when you create a many2many relation, how can you see on the "companies side" when you add them in contacts?


thx

0
Avatar
Discard
Avatar
faOtools
Best Answer

Hi,

it is better to avoid using the standard field parent_id which is of the many2one type. There is logic which relies upon that, and you would spent quite much time to recover it.

It is better to introduce 2 new fields of many2many:

contact_m2m_ids = fields.Many2many(
    "res.partner",
    "rel_table",
    "direct_rel_id",
    "back_rel_id",
    string="Contacts"
)
parents_m2m_ids = fields.Many2many(
    "res.partner",
    "rel_table",
    "back_rel_id",
    "direct_rel_id",   
    string="Companies"
)

Basically, many2many fields are based on a intermediary table, which might be used for both direct and backward relation. In the example it is the table 'rel_table', which has 2 columns 'back_rel_id' & 'direct_rel_id'. It is possible to avoid defining 'rel_table': but if you have 2 relational fields for the same model, it will not work. For more info look at: https://www.odoo.com/documentation/10.0/reference/orm.html#relational-fields

If you want to use 'parent_id' anyway, then it is better to make a computed m2m field, which would use it but would not redefine it:

@api.multi
@api.depends("parent_id")
def _compute_m2m_parents_ids(self):
    for partner in self:
        if partner.parent_id:
            partner.m2m_parents_ids = [(6, 0, [partner.parent_id.id])]
        else:
            partner.m2m_parents_ids = [(6, 0, [])]            
m2m_parents_ids = fields.Many2many(   
"res.partner",   
"rel_table",    
"back_rel_id",    
"direct_rel_id",      
string="Companies",
    compute=_compute_m2m_parents_ids,
    store=True,
)
2
Avatar
Discard
wizardz
Author

thank you

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 open a wizard after contact save ?
wizard contacts res.partner odoo10
Avatar
0
Apr 18
3389
How do I associate a contact to several Companies
contacts companies
Avatar
0
Jul 23
2496
Need help with [v15] ValueError: Invalid field 'total_due' on model 'res.partner' Solved
contacts res.partner
Avatar
Avatar
1
Jun 23
5199
Extending res.partner - first custom field worked, second raises "ProgrammingError: column ... does not exist" - what did I do wrong? Solved
res.partner odoo10
Avatar
Avatar
Avatar
Avatar
Avatar
8
Apr 23
19901
Partner_id name, name
contacts res.partner
Avatar
Avatar
1
Jun 22
7928
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