当前位置: 代码迷 >> python >> 从cron odoo更新字段
  详细解决方案

从cron odoo更新字段

热度:3   发布时间:2023-07-14 08:58:37.0

我想通过另一个字段模型更新一个字段

例如,当我选择一名员工时,我想在(hr.employee)中更新其坐标

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




    def _cron_transfert(self):
        current_date = datetime.now() 
        transfer = self.env['hr.employee.transfer'].search([('date_transfer' ,'>', current_date.strftime('%Y-%m-%d')),('state','=','done')])
        for employee in transfer: 
            employee.address_id = transfer.address_id_new 
            employee.job_id = transfer.job_id_new
            print '-------------------',transfer.job_id_new    


    date_cration = fields.Date(string='Date order', required=True, default=datetime.today())
    date_transfer = fields.Date(string='Date of actual transfer' ,required=True)
    employee_id = fields.Many2one('hr.employee',string='Employee', required=True)
    country_work_id = fields.Many2one('res.country',string='Country', required=True)
    country_id = fields.Many2one('res.country',string='Nationality',  required=True)   
    job_id = fields.Many2one('hr.job',string='Job title',  required=True) 
    state_work_id = fields.Many2one('res.country.state',string='Fed. State', domain="[('country_id', '=', country_work_id)]",  required=True)   
    working_address = fields.Many2one('res.partner',string='Working Address',  required=True)   

    job_id_new = fields.Many2one('hr.job',string='Job title',  required=True) 
    address_id_new = fields.Many2one('res.partner', string='Working Address new',  required=True)




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

我想通过此address_id_new更改字段address_id (在'hr.employee中),并通过此job_id_new更改字段job_id (在'hr.employee中)

我在Cron(XML)中添加了函数_cron_transfert,但是如果手动运行时此错误消失了(ValueError:预期的单例:hr.employee.transfer(37,38)

像这样改变你的功能

    def _cron_transfert(self):
        current_date = datetime.now() 
        transfer = self.env['hr.employee.transfer'].search([('date_transfer' ,'>', current_date.strftime('%Y-%m-%d')),('state','=','done')])
        for employee in transfer: 
            employee.address_id = employee.address_id_new.id 
            employee.job_id = employee.job_id_new.id
            print '-------------------',transfer.job_id_new  

感谢所有我找到的解决方案,它是

def _cron_emp(self):
    current_date = datetime.now() 
    record= self.env['hr.employee.transfer'].search([('date_transfer' ,'<=', current_date.strftime('%Y-%m-%d')),('state','=','done')])
    for rec in record: 
        rec.employee_id.job_id= transfer.job_id_new
        rec.employee_id.address_id = transfer.address_id_new