Odoo Help

Welcome!

This community 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.

0

How to fill up Many2Many after user took an option from a selection?

By
Yenthe
on 1/31/15, 10:40 AM 2,682 views

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

Settings/Database Structure is not similar?

zbik
on 1/31/15, 2:30 PM

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.

Yenthe
on 2/1/15, 4:47 AM
3

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 2/1/15, 9:58 AM

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

 

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.

Yenthe
on 2/1/15, 10:45 AM

Answer updated

zbik
on 2/1/15, 10:53 AM

updated again

zbik
on 2/1/15, 11:08 AM

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?

Yenthe
on 2/1/15, 11:13 AM

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

zbik
on 2/1/15, 11:32 AM

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

Yenthe
on 2/1/15, 1:07 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 1/31/15, 10:40 AM
Seen: 2682 times
Last updated: 3/16/15, 8:10 AM