This question has been flagged

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

Avatar
Discard

Settings/Database Structure is not similar?

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.

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

 

Avatar
Discard
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.

Answer updated

updated again

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?

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

Author

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