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

update fields on write() method

By
philip
on 9/6/14, 4:41 PM 10,525 views

Hi all,

Take a look at my error please... in my model I have create() method to concatenate my first_name and last_name and fill into my name field and it works. I have also write() method to update if I have any changes to first_name, last_name and fill into my name field, but I got an errors below. someone can help me figure out to work my write() method, Thanks for the effort and answers

errors------

Server Traceback (most recent call last): File "/home/jp/ws/openerp/web/addons/web/session.py", line 89, in send return openerp.netsvc.dispatch_rpc(service_name, method, args) File "/home/jp/ws/openerp/server/openerp/netsvc.py", line 296, in dispatch_rpc result = ExportService.getService(service_name).dispatch(method, params) File "/home/jp/ws/openerp/server/openerp/service/web_services.py", line 626, in dispatch res = fn(db, uid, *params) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 190, in execute_kw return self.execute(db, uid, obj, method, *args, **kw or {}) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 132, in wrapper return f(self, dbname, *args, **kwargs) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 199, in execute res = self.execute_cr(cr, uid, obj, method, *args, **kw) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 187, in execute_cr return getattr(object, method)(cr, uid, *args, **kw) TypeError: write() got multiple values for keyword argument 'context'

python -----


    def create(self, cr, uid, vals, context=None):
        name = str(vals['first_name'] or '') + ' ' +str(vals['last_name'] or '')
        vals['name'] = name.title()
        return super(hr_employee, self).create(cr, uid, vals, context=context)

    def write(self, cr, uid, vals, context=None):
        name = str(vals['first_name'] or '') + ' ' +str(vals['last_name'] or '')
        vals['name'] = name
        return super(hr_employee, self).write(cr, uid, vals, context=context)

    def onchange_first_name(self, cr, uid, ids, first_name, context=None):

        if first_name:
            return {'value': {'first_name': first_name.title()}}
        return {'value':{}}

    def onchange_last_name(self, cr, uid, ids, last_name, context=None):
        if last_name:
            return {'value': {'last_name': last_name.title()}}
        return {'value':{}}

    def onchange_name(self, cr, uid, ids, last_name, context=None):
        if last_name:
            return {'value': {'last_name': last_name.title()}}
        return {'value':{}}
            
    _columns = {
        'name': fields.char('First Name', size=32),
        'date_start': fields.date('Start Date', required=True),
        'date_end': fields.date('End Date'),
        'attendance_ids' : fields.one2many('philcode.test', 'calendar_id', 'Working Time'),
        'first_name': fields.char('First Name', size=32, required=True),
        'last_name': fields.char('Last Name', size=32, required=True),
    }

xml -----

                        <label for="first_name"/>
                        <div>
                            <h1><field name="first_name" style="width: 50%%" on_change="onchange_first_name(first_name)"/></h1><br/>
                        </div>
                        <label for="last_name"/>
                        <div>
                            <h1><field name="last_name" style="width: 50%%" on_change="onchange_last_name(last_name)"/></h1>
                        </div>
                        <label for="name" String="Full Name"/>
                        <div>
                            <h1><field name="name" on_change="onchange_name(name)" style="width: 50%%"/></h1>

1

deep

--deep--
1617
| 5 4 7
Bengaluru, India
--deep--

Techno-Functional Associate with 6+ years of experience in Odoo (formerly known as OpenERP).

deep
On 9/8/14, 4:09 AM

parameter specified for Write method are wrong

def write(self, cr, uid, ids, vals, context=None):

Ids was not specified...

Thanks for the response, so what's right for the write method
and how to specify the ids?

philip
on 9/8/14, 8:29 AM

U need not pass anything for Ids ... it is exclusively passed by openerp .....
It is d necessary syntax for it ... as it is orm method...

Sent from Yahoo! Mail on Android



From: philip <johnpaul@ictsecuritygroup.org>;
To: deep <deepa4lok@yahoo.com>;
Subject: Re: False
Sent: Mon, Sep 8, 2014 12:29:54 PM

Thanks for the response, so what's right for the write method
and how to specify the ids?

--
philip
Sent by OpenERP S.A. using Odoo about Forum Post False
deep
on 9/8/14, 9:13 AM
1
Mariusz Mizgier
On 9/7/14, 5:11 AM

You don't provide additional parameter for the write method, which is ids of the written record. If OpenERP has to write, it needs to know to which record it should write to.

I tried to put ids in my function but I got error mesaage:
name = str(vals['first_name'] or '') + ' ' +str(vals['last_name'] or '') KeyError: 'last_name'
python code def write(self, cr, uid, vals, context=None): name = str(vals['first_name'] or '') + ' ' +str(vals['last_name'] or '') vals['name'] = name return super(hr_employee, self).write(cr, uid, vals, context=context)

philip
on 9/7/14, 6:27 AM

here is my python code

def write(self, cr, uid, ids, vals, context=None): name = str(vals['first_name'] or '') + ' ' +str(vals['last_name'] or '') vals['name'] = name return super(hr_employee, self).write(cr, uid, ids, vals, context=context)

can you point me to the right direction, I don't have any idea on write() method.

Thanks for the response

philip
on 9/7/14, 6:30 AM

It means, that you don't have key of 'last_name' in your vals dictionary - you should first do the check for whether first_name or last_name are the values that are changing on the current record and based on that then create name you want to put, ie. if 'first_name' in vals and 'last_name' in vals: name = str(vals['first_name'] + str(vals['last_name']) if 'first_name' in vals and 'last_name' not in vals: do something.... (like check if record has got already its last name and you have to do your text operation on new first name, but old last name)

Currenda Sp. z o.o., Mariusz Mizgier
on 9/7/14, 8:49 AM

can you fix the code

def write(self, cr, uid, vals, context=None):
name = str(vals['first_name'] or '') + ' ' +str(vals['last_name'] or '')
vals['name'] = name
return super(hr_employee, self).write(cr, uid, vals, context=context)

Thank really appreciate all your help

philip
on 9/7/14, 10:38 AM

Try this: def write(self, cr, uid, ids, vals, context=None): employee_data = self.pool.get('hr.employee').browse(cr, uid, ids) if vals['first_name'] and vals['last_name']: name = str(vals['first_name']) + ' ' +str(vals['last_name']) vals['name'] = name return super(hr_employee, self).write(cr, uid, vals, context=context) if vals['first_name']: name = str(vals['first_name']) + ' ' +str(employee_data.last_name or '') vals['name'] = name return super(hr_employee, self).write(cr, uid, vals, context=context) if vals['second_name']: employee_data = self.pool.get('hr.employee').browse(cr, uid, ids) name = str(employee_data.first_name or '') + ' ' +str(vals['second_name']) vals['name'] = name return super(hr_employee, self).write(cr, uid, vals, context=context)

Currenda Sp. z o.o., Mariusz Mizgier
on 9/7/14, 2:23 PM

Thanks will try this later, is this indentation is right?
def write(self, cr, uid, ids, vals, context=None):
----employee_data = self.pool.get('hr.employee').browse(cr, uid, ids)
----if vals['first_name'] and vals['last_name']:
----name = str(vals['first_name']) + ' ' +str(vals['last_name']) vals['name'] = name
----return super(hr_employee, self).write(cr, uid, vals, context=context) if vals['first_name']:
----name = str(vals['first_name']) + ' ' +str(employee_data.last_name or '') vals['name'] = name
----return super(hr_employee, self).write(cr, uid, vals, context=context) if vals['second_name']:
----employee_data = self.pool.get('hr.employee').browse(cr, uid, ids)
----name = str(employee_data.first_name or '') + ' ' +str(vals['second_name'])
----vals['name'] = name return super(hr_employee, self).write(cr, uid, vals, context=context)

philip
on 9/7/14, 11:15 PM

No, it is not right - comments are not working well with the indents: ----def write(self, cr, uid, ids, vals, context=None): --------employee_data = self.pool.get('hr.employee').browse(cr, uid, ids) --------if vals['first_name'] and vals['last_name']: ------------name = str(vals['first_name']) + ' ' +str(vals['last_name']) vals['name'] = name ------------return super(hr_employee, self).write(cr, uid, vals, context=context) --------if vals['first_name']: ------------name = str(vals['first_name']) + ' ' +str(employee_data.last_name or '') ------------vals['name'] = name ------------return super(hr_employee, self).write(cr, uid, vals, context=context) --------if vals['second_name']: ------------name = str(employee_data.first_name or '') + ' ' +str(vals['second_name']) ------------vals['name'] = name ------------return super(hr_employee, self).write(cr, uid, vals, context=context) --------else: ------------return super(hr_employee, self).write(cr, uid, vals, context=context)

Currenda Sp. z o.o., Mariusz Mizgier
on 9/8/14, 1:44 AM

Thanks will try later. thanks for your response

philip
on 9/8/14, 3:18 AM

PYTHON-----
----def write(self, cr, uid, ids, vals, context=None):
--------employee_data = self.pool.get('hr.employee').browse(cr, uid, ids)
--------if vals['first_name'] and vals['last_name']:
------------name = str(vals['first_name']) + ' ' +str(vals['last_name'])
------------vals['name'] = name
------------return super(hr_employee, self).write(cr, uid, vals, context=context)
--------if vals['first_name']:
------------name = str(vals['first_name']) + ' ' +str(employee_data.last_name or '')
------------vals['name'] = name
------------return super(hr_employee, self).write(cr, uid, vals, context=context)
--------if vals['second_name']:
------------name = str(employee_data.first_name or '') + ' ' +str(vals['second_name'])
------------vals['name'] = name
------------return super(hr_employee, self).write(cr, uid, vals, context=context)
--------else:
------------return super(hr_employee, self).write(cr, uid, vals, context=context)


ERRORS------ Server Traceback (most recent call last):
--File "/home/jp/ws/openerp/web/addons/web/session.py", line 89, in send
----return openerp.netsvc.dispatch_rpc(service_name, method, args)
--File "/home/jp/ws/openerp/server/openerp/netsvc.py", line 296, in dispatch_rpc
----result = ExportService.getService(service_name).dispatch(method, params)
--File "/home/jp/ws/openerp/server/openerp/service/web_services.py", line 626, in dispatch
----res = fn(db, uid, *params)
--File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 190, in execute_kw
----return self.execute(db, uid, obj, method, *args, **kw or {})
--File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 132, in wrapper
----return f(self, dbname, *args, **kwargs)
--File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 199, in execute
----res = self.execute_cr(cr, uid, obj, method, *args, **kw)
--File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 187, in execute_cr
----return getattr(object, method)(cr, uid, *args, **kw)
--File "/home/jp/ws/openerp/my_addons/for_development/philcode_test.py", line 15, in write
----name = str(vals['first_name']) + ' ' +str(vals['last_name'])
KeyError: 'last_name'

philip
on 9/8/14, 8:27 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

1 follower(s)

Stats

Asked: 9/6/14, 4:41 PM
Seen: 10525 times
Last updated: 1/4/16, 8:56 PM