3

我想在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'这样的错误,请指导我如何消除这个错误?

4

1 回答 1

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  

与其调用read方法不如调用browse()方法,您可以阅读Odoo 8 中的 Browse() 与 read() 性能

您的代码应该是:

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)
于 2016-12-28T08:11:17.370 回答