问题
I have module which has three fields •name •first name •last name When a user press save The first name and last name will concatenate and displayed in name field. Name Field must be in read only mode.
def onchange_name(self, cr, uid, ids, firstname, lastname, context=None):
value = {'fullname' : True}
if firstname and lastname:
value['fullname'] = firstname + " " +lastname
return {'value': value}
<field name="fullname" readonly="True" on_change="onchange_fullname(fullname,context)"/>
<field name="firstname" string="First name" on_change="onchange_name(firstname,lastname,context)"/>
<field name="lastname" string="Last name" on_change="onchange_name(firstname,lastname,context)"/>
回答1:
In the model, redefine the name field as computed and stored:
name = fields.Char(compute='comp_name', store=True)
then define the compute method:
@api.depends('first_name','last_name')
def comp_name(self):
self.name = (self.first_name or '')+' '+(self.last_name or '')
this way you can remove the on_change
回答2:
def create(self, cr, uid, vals, context=None):
name = str(vals['first_name'] or '') + ' ' +str(vals['last_name'] or '')
vals['name'] = name
return super(sample_model, self).create(cr, uid, vals, context=context)
来源:https://stackoverflow.com/questions/35308194/concatenate-firstname-and-lastname-and-fill-into-name-field-in-odoo