I have two fields. I want to show the sum of these values in the field 'name' which will be 'readonly'.
I tried like this:
@api.onchange('first_name', 'last_name')
def onchange_name(self):
if self.first_name or self.last_name:
self.field_name = self.first_name self.last_name
#self.name = self.first_name self.last_name
name = fields.Char(string='Name of the Emplyee', readonly=True, )
first_name = fields.Char(string='First Name')
last_name = fields.Char(string='Last Name')
But it's giving me error: 'TypeError: can only concatenate str (not "bool") to str'
.
How can I fix this?
CodePudding user response:
I think, you don't have set a value for your two fields. Basically, if the fields.Char is empty, the value is False.
Try it with a str cast:
if self.first_name or self.last_name:
self.field_name = str(self.first_name) str(self.last_name)
You can also change the "or" operator by "and" to fix it another way.
Regards,
CodePudding user response:
First, you need to check the value of the fields
@api.onchange('first_name', 'last_name')
def onchange_name(self):
if self.first_name and self.last_name:
self.name = str(self.first_name) '' str(self.last_name)
elif self.first_name and not self.last_name:
self.name = str(self.first_name)
elif self.last_name and not self.first_name:
self.name = str(self.last_name)
else:
self.name = None
And remove readonly from the name field and put readonly in the view(.xml) file. so you don't get a 'FALSE' value in the name field.
in Model file (.py)
name = fields.Char(string='Name of the Emplyee')
in view file (.xml)
<field name='name' readonly="1"/>