如何覆盖odoo 10中的创建方法

How to override create methode in 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' 所以请指导我如何删除此错误?

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

read([fields])

读取自身记录的请求字段,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  

与其调用read方法不如调用browse()方法,你可以阅读Browse() vs read() performance in Odoo 8

您的代码应该是:

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)