如何在odoo 10中覆盖create method

时间:2016-12-28 06:51:31

标签: openerp odoo-8 openerp-8 odoo-10

我想在odoo 10中使用相同的创建方法,如下所示,我想在odoo 10中转换下面的代码,below code适用于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)

我正在尝试下面的代码:

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)

但它给我的错误如TypeError: read() got multiple values for keyword argument 'fields'所以请指导我如何删除此错误?

1 个答案:

答案 0 :(得分:3)

read方法接受fields作为参数,你给它两个参数。

read([fields])

以self,low-level / RPC方法读取记录的请求字段。在Python代码中,首选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  

您可以阅读Browse() vs read() performance in Odoo 8

,而不是调用read方法,最好调用browse()方法

您的代码应为:

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)