Skip to Content
Menu
This question has been flagged
2 Replies
34009 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>

Avatar
Discard
Best Answer

parameter specified for Write method are wrong

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

Ids was not specified...

Avatar
Discard
Author

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

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
Best Answer

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.

Avatar
Discard
Author

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)

Author

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

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)

Author

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

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)

Author

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)

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)

Author

Thanks will try later. thanks for your response

Author

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'

Related Posts Replies Views Activity
1
Aug 20
3522
1
Apr 20
4068
1
Sep 19
7707
3
Feb 19
7489
3
Jul 17
10373