How do you concatenate first name and last name in python?

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}



                    
   

asked Feb 10, 2016 at 6:19

2

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

answered Feb 10, 2016 at 9:23

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]

answered Feb 10, 2016 at 6:37

0

Thanks for your subscription!

In partner form and in contacts form view, while creating a new partner I made two extra fields firstname and lastname. The concatenation of firstname and lastname should be automatically filled in 'name' field . This should be achieved without using the name filed as functional field.

After filling either firstname,lastname or both, name should be filled by clicking Save button.

Whether it is possible to inherit the create/write method?

4Answers

Yes you can override create and write Method:-

Example Create Method:-

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]

Here is the solution. The below code updates the name field as required while creating a new partner.

def create[self, cr, uid, vals, context=None]: new_id=super[partner, self].create[cr, uid, vals, context=context] names = [vals['first_name'], vals['last_name']] fullname = " ".join[[s for s in names if s]] vals['name'] = fullname return new_id

def create[self, cr, uid, vals, context=None]:
        res = super[sample_model, self].create[cr, uid, vals, context=context]
        name = str[vals['first_name'] or ''] + ' ' +str[vals['last_name'] or '']
        vals['name'] = name
        return res

where sample_model is your class name in which you're trying to make your model 'sample.model' this function should be written inside your class sample_model.

Hi Sureka,

You can try with onchange function:

ie,

'first_name': fields.char['First Name', size=32],
'last_name': fields.char['Last Name', size=32],

In xml:



onchange function:

def onchange_first_last[self, cr, uid, ids, first_name, last_name, context=None]:
        v = {}
        if first_name and last_name:
            v['name'] = first_name+last_name
        return {'value': v}

How do you combine names in Python?

Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.

How do you concatenate two inputs in Python?

Python String Concatenation can be done using various ways..
Using + operator..
Using join[] method..
Using % operator..
Using format[] function..
Using f-string [Literal String Interpolation].

How do you concatenate elements in Python?

Using join[] method to concatenate items in a list to a single string. The join[] is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.

What is the best way to concatenate strings in Python?

One of the most popular methods to concatenate two strings in Python [or more] is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.

Chủ Đề