concatenate firstname and lastname and fill into name field in odoo

限于喜欢 提交于 2020-01-16 01:17:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!