从其他模型odoo更新模型

时间:2018-09-10 09:18:48

标签: python updates odoo-10

我想用另一个模型更新一个模型

例如,当我选择一名员工时,我想在完成状态后更新其坐标

    class Hr_transf_employee(models.Model):
        _name = 'hr.employee.transfer'
        _rec_name = 'employee_id'



        date_cration = fields.Date(string='Date order', required=True, default=datetime.today())
        date_transfer = fields.Date(string='Date transfer')
        employee_id = fields.Many2one('hr.employee',string='Employee', required=True) 

        job_id_new = fields.Many2one('hr.job',string='Job title', required=True) 
        country_work_id_new = fields.Many2one('res.country', 'Country work new') 

        state = fields.Selection([
            ('draft', 'Draft'),
            ('accept', 'Accept'),
            ('done', 'Done'),
            ('cancel', 'Cancel'),
        ], string='Order Status', readonly=True, copy=False, store=True, default='draft')

我想通过 job_id_new 更新 job_id 国家 country_work_id_new 在模型hr.employee

2 个答案:

答案 0 :(得分:1)

第一个选项: 您可以覆盖hr.employee.transfert上的“写入”方法,并更新employee_id字段

@api.multi
def write(self, vals):
    res = super(Hr_transf_employee, self).write(vals)

    if vals.get('state', False) == 'done':  # Only update employee if the transferts become 'done'.
        for transfert in self:
            transfert.employee_id.job_id = transfert.job_id_new
            transfert.employee_id.country = transfert.country_work_id_new
    return res

第二个选项:
您可以在res.partner模型上添加两个计算字段(您可以对两个字段使用相同的方法)。

@api.multi
def my_compute_method(self):
    for employee in self:
        employee_transfert_id = self.env['hr.employee.transfer'].search([
            ('employee_id', '=', employee.id),
            ('state', '=', 'done'),
        ], order="write_date DESC", limit=1)  # Get the last done transfert

        employee.job_id = employee_transfert_id and employee_transfert_id.job_id_new or False
        employee.country = employee_transfert_id and employee_transfert_id.country_work_id_new or False

答案 1 :(得分:1)

将完成的功能wkf编辑为:

@api.multi
def done(self):
    self.state = 'done'
    if self.job_id_new :
       self.job_id = self.job_id_new.id
    if self.country_work_id_new :
       self.country = self.country_work_id_new.id

今天开始:

if  self.date_transfer <= self.create_date : 
    self.date_transfer = fields.datetime.now()