Skip to Content
Menu
This question has been flagged
2 Replies
2248 Views

Hello, I have one field developer technology and it is many2many field and I want to export data from that field. so I want to convert this m2m field into a comma-separated string, for example, I want python,php,react to this kind of data.

How achieve this?

Avatar
Discard
Best Answer

Hi,

Create a new field to store a comma-separated string and update it using the onchange method.


m2m_field = fields.Many2many('hr.department')

comma_separated_field = fields.Char('Comma Separated Field')

@api.onchange('m2m_field')
def onchange_m2m_field(self):
self.comma_separated_field = ', '.join(self.m2m_field.mapped('name')) if self.m2m_field else False



Hope it Helps,

Kiran K



Avatar
Discard
Best Answer

Hello, 

Use a computed field 


other_model_ids = fields.Char('other_model')
other_model_txt = fields.Text(compute='_compute_other_model_txt')

@api.depends('other_model_ids')
def _compute_other_model_txt(self):
​for rec in self:
​​rec.other_model_txt = [x.name for x in rec.other_model_ids]
​ ​# assuming name is the attr You wante to get

Avatar
Discard