How to override create methode in odoo 10

4.8k views Asked by At

i want to use same create method in odoo 10 as below means i want to convert below code in odoo 10, below code is working well for odoo 8

def create(self, cr, uid, vals, context=None):
    phase_obj = self.pool.get('hr_evaluation.plan.phase')
    survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]

    if vals.get('user_id'):
        user_obj = self.pool.get('res.users')
        partner_id = user_obj.read(cr, uid, vals.get('user_id'), fields=['partner_id'], context=context)['partner_id'][0]
    else:
        partner_id = None

    user_input_obj = self.pool.get('survey.user_input')

    if not vals.get('deadline'):
        vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)

    ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
                                          'deadline': vals.get('deadline'),
                                          'type': 'link',
                                          'partner_id': partner_id}, context=context)
    vals['request_id'] = ret
    return super(hr_evaluation_interview, self).create(cr, uid, vals, context=context)

i am trying below code:

def create(self, vals):
    survey_id = self.env['hr_evaluation.plan.phase'].read(vals.get('phase_id'),fields=['survey_id'])['survey_id'][0]

    if vals.get('user_id'):
        partner_id = self.env['res.users'].read(vals.get('user_id'), fields=['partner_id'])['partner_id'][0]
    else:
        partner_id = None

    if not vals.get('deadline'):
        vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)

    ret = self.env['survey.user_input'].create({'survey_id': survey_id,
                                          'deadline': vals.get('deadline'),
                                          'type': 'link',
                                          'partner_id': partner_id})
    vals['request_id'] = ret
    return super(hr_evaluation_interview, self).create(vals)

but it is giving me error like TypeError: read() got multiple values for keyword argument 'fields' so please guide me how can i remove this error?

1

There are 1 answers

4
Kenly On BEST ANSWER

read method accept fields as argument and you give it two arguments.

read([fields])

Reads the requested fields for the records in self, low-level/RPC method. In Python code, prefer browse().

Parameters
    fields -- list of field names to return (default is all fields)
Returns
    a list of dictionaries mapping field names to their values, with one dictionary per record
Raises
    AccessError -- if user has no read rights on some of the given records  

Instead of calling read method it's better to call browse() method, you can read Browse() vs read() performance in Odoo 8

Your code should be:

def create(self, vals):
    survey_id = self.env['hr_evaluation.plan.phase'].browse(vals.get('phase_id'))

    if vals.get('user_id'):
        partner_id = self.env['res.users'].browse(vals.get('user_id'))
    else:
        partner_id = None

    if not vals.get('deadline'):
        vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)

    ret = self.env['survey.user_input'].create({'survey_id': survey_id.id,
                                          'deadline': vals.get('deadline'),
                                          'type': 'link',
                                          'partner_id': partner_id.id})
    vals['request_id'] = ret.id
    return super(hr_evaluation_interview, self).create(vals)