如何将值从one2many字段传递到另一个具有相同形式的模型的字段?下方显示的表单查看图片(ial)

时间:2018-11-11 15:41:18

标签: xml python-2.7 odoo-8

ial

one2many field name: survey_request_ids
model of one2many field : hr_evaluation.evaluation
Field name of 'Interviewer' inside one2many : user_id
model of 'Interviewer' inside one2many : hr.evaluation.interview

我想在“评估截止日期”下创建一个新字段“采访者” 它将具有来自one2many中以下字段“ Interviewer”的值。

我尝试过这些,

from openerp import models,fields,api

class hr_evaluation_interview(models.Model):
    _inherit = 'hr.evaluation.interview'

    @api.onchange('user_id')
    def _onchange_user_id(self):
        rec = self.env['hr_evaluation.evaluation'].search([('id','=', 
self.evaluation_id.id)])
        rec.update({
            'interviewer_ids': [(4,self.user_id.id)]
        })


class hr_evaluation_inherit(models.Model):
    _inherit = 'hr_evaluation.evaluation'

    _columns = {
        'interviewer_ids': fields.many2many('res.users', 
string='Interviewer',readonly=True)
    }

1 个答案:

答案 0 :(得分:2)

您可以使用计算的many2many字段。计算方法将遍历所有评估表,并创建一个记录集,其中包含以所有形式出现的所有采访员。然后用该记录集填充many2many字段。

它看起来像这样:

def _compute_interviewer_ids(self):
    for record in self:
        interviewer_ids = []
        for appraisal_form in record.survey_request_ids:
            interviewer_ids.append(appraisal_form.interviewer_id.id)
        record.interviewer_ids = self.env['interviewer.model'].browse(interviewer_ids)


interviewer_ids = fields.Many2many('interviewer.model', compute='_compute_interviewer_ids', string='Interviewers')

编辑:这是一个完整的模型及其子模型,因此您可以在上下文中查看它。在v12中进行了测试。

from odoo import models, fields, api

class ExampleModel(models.Model):
    _name = 'example_module.example_model'
    _description = 'Example Model'

    def _compute_interviewer_ids(self):
        for record in self:
            interviewer_ids = []
            for appraisal_form in record.submodel_ids:
                interviewer_ids.append(appraisal_form.partner_id.id)
            record.interviewer_ids = self.env['res.partner'].browse(interviewer_ids)

    name = fields.Char()
    description = fields.Text()
    submodel_ids = fields.One2many('example_module.example_sub_model', 'examplemodel_id', 'SubModel Records')
    interviewer_ids = fields.Many2many('res.partner', compute='_compute_interviewer_ids', string='Interviewers')

class ExampleSubModel(models.Model):
    _name = 'example_module.example_sub_model'
    _description = 'Example Sub-model'

    name = fields.Char()
    examplemodel_id = fields.Many2one('example_module.example_model', 'Parent Record')
    partner_id = fields.Many2one('res.partner', 'Partner')