Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

concatenate firstname and lastname and fill into name field

By
GG
on 12/18/13, 10:15 AM 3,132 views

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?

3

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 12/19/13, 2:53 AM

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)

Hi Prakash, thanks for your answer.I tried the above code. I entered the first name and lastname and clicked Save button in partner form. It points to the name field because it is mandatory. Rather if I enter a single character in name field then click on Save button, it updates with concatenated string of both firstname n name. Is there any possibility to update without entering a single character ?

GG
on 12/19/13, 4:55 AM
1
GG
On 12/24/13, 4:12 AM

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

0
Abhishek H Menon
On 12/19/13, 5:36 AM
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.

0
Jasad
On 12/19/13, 5:30 AM

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:

<field name="first_name" on_change="onchange_first_last(first_name, last_name, context)"/>
<field name="last_name" on_change="onchange_first_last(first_name, last_name, context)"/>

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}

Jasad thanks for your answer. I already included the on_change method in the model. It also works perfect only for the existing partner if I change either firstname or lastname it updates the name.

GG
on 12/19/13, 5:35 AM

My question is while creating a new partner, it should concatenate the first name and lastname and to be filled in name field.

GG
on 12/19/13, 5:36 AM

Check for the below answer

Abhishek H Menon
on 4/11/14, 2:48 AM

Hi, is this code apply to name on hr.employee model? I try the above code but Im getting error: "TypeError: Cannot read property 'value' of null" Thanks

philip
on 9/4/14, 10:26 AM

Error occur when I entered value in first_name field then pressing enter or tab key. can anyone figure it out? Thanks

philip
on 9/4/14, 10:52 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 12/18/13, 10:15 AM
Seen: 3132 times
Last updated: 3/16/15, 8:10 AM