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 fields - how to implement

Subscribe

Get notified when there's activity on this post

This question has been flagged
2 Replies
51308 Views
Avatar
karlos

I have the table: Employees, which contains only the field 'name' and 'login' (it's associated to the table users)

class Employees(osv.osv):
    _name = 'Employees.Employees'
    _columns = {
         'the_name':fields.char('Name:', size = 50),
         'user_id': fields.many2one('res.users', 'User:', select = True),
     }
Employees()

Example:

'John' -> 'John@gmail.com'

'Marilyn' -> 'marilyn@outlook.com'

Then I have the table: Tasks, which contains the fields 'name', 'description', and 'employees_id'. When a task is created it could contain several employees for the same task, that's why I choose the "many2many" because I could select several employees. So, I have tried the following:

class tasks(osv.osv):
    _name = 'tasks.tasks'
    _columns = {
          'the_name':fields.char('Name', size = 50),
          'description':fields.text('Description'),
          'employees_id':fields.many2many('Employees.Employees', 'Employees', '???', 'user_id', 'All Employees')
     }
tasks()

Example:

'Carry sand' -> 'Carry the whole sand from the beach' -> 'john@gmail.com; marilyn@outlook.com'

'Play' -> 'Play with legos' -> 'john@gmail.com'

But I don't know what to put on "???"..thanks.

0
Avatar
Discard
Avatar
Simplify it!
Best Answer

You have to do this:

'employees_ids': fields.many2many('Employees.Employees', 'tasks_employees_rel', 'task_id', 'employee_id', 'Employees assigned to task')

To give you a better example.

A employee can be assigned to many tasks

A task can be assigned to many employees

So you have a many2many relation, so that means that you have to create a new table containing both keys.

That what you do with this. You say that you are having a many2many relation with employees so you have to write the first arg the other object name.

Then you have to write which table is going to contain the foreign keys, so you write some descriptive table.

Then you need to give a name to your first key, since you are calling it from tasks you have to write tasks_id.

And then you need to give a name to the other object key.

Finally you have to give the field a name, and that's all.

3
Avatar
Discard
karlos
Author

I'm editing my answer..

Avatar
karlos
Author Best Answer

@Grover Menacho, I was quite sure I needed to create a third table, thanks for warning me that, but here's my doubts.

1) Should I create the third table by code dynamically (if so, how?) or I can go to "pgAdmin III" (postgreSQL management) and create by hand? I think the best approach is dynamically, but I don't know how to create it dynamically to execute in the load of the module.

2) So, with the third table I should have something like this:

  class Employees(osv.osv):
                    _name = 'Employees.Employees'
                    _columns = {
                         'the_name':fields.char('Name:', size = 50),
                         'user_id': fields.many2one('res.users', 'User:', select = True),
                     }
                Employees()

-> Table 'Employees' -> fields: the_name, user_id

class tasks(osv.osv):
    _name = 'tasks.tasks'
    _columns = {
          'the_name':fields.char('Name', size = 50),
          'description':fields.text('Description'),
          'employees_id':fields.many2many('Employees.Employees', 'tasks_employees_rel',  'employee_id', 'task_id', 'All Employees')
    } tasks()

-> table 'Tasks' -> fields: the_name, description (I think employees_id isn't created in the database..Am I wrong??)

And then I need to have a third table, called: tasks_employees_rel, which contains: id, employee_id, task_id...?

0
Avatar
Discard
karlos
Author

I've tried this, and I think it worked..because in the database, table 'tasks_employees_rel', the data is being insert. However, I want to make a rule for this. The user A can only view the tasks assigned to his own profile employee. Remember that, whenever I create a employee I assign them a user.

I was trying something like: [('tasks_employees_rel.employee_id, '=', user.id)], without success.

karlos
Author

I think I'd just solved this too. I will post my resolution later if I'm 100% sure about it. Thanks.

karlos
Author

The resolution above worked.

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
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