This question has been flagged
1 Reply
2126 Views

Hi all.

I am loosing my hair in a so simple operation.

In ResPartner I have name like "corp_town1" , "corp_town2" 

I want to create a new compte field split_name who'll contain

 name.split('_')[0]  (only corp)


from odoo import models, fields, api


class ResPartner(models.Model):
_inherit = 'res.partner'
split_name = fields.Char(compute="_split_name", store=True)
#
@api.depends('name')
def _split_name(self):
for r in self:
if not r.name:
r.split_name=""
else:
init_name=r.name
self.split_name = init_name.split('_')[0]


Any help welcome 

regards

Avatar
Discard
Author Best Answer

HI.

I manage to create the x_split_name field with the interface

contact ->  dev tools -> view field create new:

field name :x_split_name

field_type : char

related_fiels dependancies : name

compute : 

for record in self:
     record['x_split_name'] = record.name.split('_')[0]


Avatar
Discard