Help

0

Autofill Many2many field with compute? Odoo 9 (New API)

Avatar
Jorge

Hello guys, I'd like create a function for autofill a many2many field with the registers of a model but my many2many field is always empty, this is my code:


    asistentes_de_eventos = fields.Many2many('event.registration', string='Asistentes al evento', compute='_compute_asistentes_evento')       

    

    @api.multi

    def _compute_asistentes_evento(self):

        pruebaaux = []

        pruebaaux = [(4,[self.env['event.registration'].ids])]

        self.asistentes_de_eventos = pruebaaux


Could anyone help me please? Thank you so much!

Avatar
Abaikan
1 Jawaban
3
Avatar
Manish Kumar Bohra
Best Answer

Hello Jorge , 

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 

Avatar
Abaikan