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 fill up Many2Many after user took an option from a selection?

Subscribe

Get notified when there's activity on this post

This question has been flagged
domainmany2oneselectionmany2manyodoo8.0
1 Reply
18883 Views
Avatar
Yenthe Van Ginneken (Mainframe Monkey)

Hi guys

I've built a selection that gets all the modelnames from the database:

model_names = fields.Many2one('ir.model', 'Model to use')

This builds a selection where the user can see all models and choose one out. The next step that I want is that when the user selects a model from the selection that the next field (a Many2Many) is automaticly getting all fields that are in the model the user selects.
For example: the user chooses res.partner from the model_names selection. The next field (Many2Many) should now show all fields that are available in the res.partner model.

How exactly should I do this? I should access the model ir.model.fields and get every single field that matches the correct model the user selected. I assume I should create an onchange that loads all fieldnames in the Many2Many when the user selected something in the Many2one? Would this also be possible with a domain_filter?

XML file:

     <record model="ir.ui.view" id="my_form_view">
            <field name="name">myname.form</field>
            <field name="model">model.builder</field>
            <field name="arch" type="xml">
                <form string="My Form">
                    <sheet string="test">
                        <group>
                            <field name="name"/>
                <field name="model_names"/>
                <field name="fields_to_use"/>
                        </group>
             <notebook>
                            <page string="Advanced" name="Advanced">
                
                </page>
                </notebook>
            </sheet>
                </form>
            </field>
        </record>

Python code:

from openerp import models, fields, api

class MyClass(models.Model):
    _name = 'model.builder'

    name = fields.Char(string="Title", required=True)
    model_names = fields.Many2one('ir.model', 'Model to use', help='The model that you use here will give you access to all fields from this model. This will auto-generate the fields in the next dropdown.', required=True)
    fields_to_use = fields.Many2many('ir.model.fields', 'field_names_model', required=True)

    @api.one
    @api.depends(
        'model_names',
        'model_names.field_id',
        'model_names.field_id.model_id',
    )
    def _get_names(self):
        self.fields_to_use_computed = self.model_names.field_id.filtered(lambda l: l.model_id.id == self.model_names.id)    

    fields_to_use_computed = fields.Many2many('ir.model.fields', compute='_get_names', string='Fields to use COMPUTED')

    @api.onchange('fields_to_use_computed')
    def do_stuff(self):
        self.fields_to_use=self.fields_to_use_computed

I'd love some help as I have no clue here.

Thanks
Yenthe

0
Avatar
Discard
Zbik

Settings/Database Structure is not similar?

Yenthe Van Ginneken (Mainframe Monkey)
Author

When you open up a specific module and get all the field names yes.. But I would like to get all those fields from a specific model in a Many2Many so the user can select the fields from the specific model he chose in the previous selection.

Avatar
Zbik
Best Answer

Try this:

field_names = fields.Many2many('ir.model.fields', string='Fields to use', domain="[('model_id', '=', model_names )]")

but this not auto select-fill all fields and not clear previous selection, if you expect?

UPDATE:

If you need auto fill, you additionaly add, like this (not tested):

    @api.one
    @api.depends(
        'model_names',
        'model_names.field_id',
        'model_names.field_id.model_id',
    )
    def _get_names(self):
        self.field_names_computed = self.model_names.field_id.filtered(lambda l: l.model_id.id == self.model_names.id)    

    model_names = fields.Many2one('ir.model', 'Model to use')
    field_names_computed = fields.Many2many('ir.model.fields', compute='_get_names', string='Fields to use COMPUTED')   
    field_names = fields.Many2many('ir.model.fields', string='Fields to use', domain="[('model_id', '=', model_names )]")

    @api.onchange('field_names_computed')
    def do_stuff(self):
        self.field_names=self.field_names_computed

 

4
Avatar
Discard
Yenthe Van Ginneken (Mainframe Monkey)
Author

Thanks for the suggestion Zbik. Sadly the Many2many should be reloaded and filtered every time the user selects another model. The Many2many should only be filled with the fields from the model of the selection. Any idea on this? I was thinking about an on_change method but didn't get anything working yet.

Zbik

Answer updated

Zbik

updated again

Yenthe Van Ginneken (Mainframe Monkey)
Author

Thanks Zbik! I gave this a try but nothing changed.. sadly the data is still showing all fields from all fields. Even after selecting the model from the selection.. I've updated my question with the whole code I have now! Any idea?

Zbik

You missed domain="[('model_id', '=', model_names )]") in full solutions. updated again.

Yenthe Van Ginneken (Mainframe Monkey)
Author

Awesome, this works like a charm. Thank you so much zbik :) Accepted & upvoted!

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 use widget='selection' and domain in many2one field Solved
domain many2one selection
Avatar
Avatar
Avatar
2
Dec 23
33777
How to dynamically update domain filter on selection widget?
filter domain many2one selection
Avatar
Avatar
Avatar
2
Sep 23
10502
how to populate many2one field with condition
domain many2one many2many pop
Avatar
Avatar
1
Apr 18
9469
How to create a many2one based on a many2many relation? Solved
many2one selection many2many filtering
Avatar
Avatar
1
Nov 16
8992
Why is my tax not filled in when using an automated action? Solved
many2one many2many automation odoo8.0
Avatar
Avatar
1
May 15
5210
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