1
votes

I am trying to compute a field in my model to be a concatenation of the names of another model which has a many2many relationship with my model:

class p2_vm(models.Model):
    _name = 'p2.vm'
    def _get_hostfunction_names(self):
        names = []
        for id in self.hostfunction_ids:
            names.append(id.name)
        self.functions = ','.join(names)

    hostfunction_id = fields.Many2many('p2.hostfunction')
    functions = fields.Char(compute=_get_hostfunction_names)

The other model has:

class p2_hostfunction(models.Model):
    _name = 'p2.hostfunction'
    name = fields.Char('Name', required=True)
    vm_id = fields.Many2many('p2.vm')

How can I accomplish what I want?

1
just pu @api.one over _get_hostfunction_names , i should workPrakash Kumar

1 Answers

2
votes

You should decorate your compute method with @api.multi and @api.depends('field_name'), you will end up with something like this:

@api.multi
@api.depends('hostfunction_id')
def _get_hostfunction_names(self):
    for item in self:
        item.functions = ','.join([x.name for x in item.hostfunction_id])

Also, is better to declare the compute method with a string instead of passing the current object, in order to ease override in any case, like fields.Char(compute='_get_hostfunction_names').