I want to update a field by another field model
for example when I select an employee I want to update his coordinates in (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')
I want to change the field address_id (in 'hr.employee) by this address_id_new and the field job_id (in 'hr.employee) by this job_id_new
i add the function _cron_transfert in Cron (XML) but if when i clic Run Manually This error is vanishing ( ValueError: Expected singleton: hr.employee.transfer(37, 38)
Change your function like this
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
Thanks to all I found the solution and it is
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.