Skip to Content
Menu
This question has been flagged
3 Replies
6447 Views

I am newbie. Can you help me solve this problem?

My Field
employee_id = fields.Many2one(comodel_name="hr.employee", string="Employee", required=True, )

@api.model
def default_get(self):
res=super(Employee,self).default_get('employee_id')
return res
Avatar
Discard
Best Answer
 @api.depends('user_id.employee_ids')
 def _compute_employee_id(self):
        self.employee_id = self.user_id.employee_ids and self.user_id.employee_ids[0].id or False

user_id = fields.Many2one('res.users','User',default=lambda self: self.env.uid,readonly=True)
employee_id = fields.Many2one('hr.employee', string="Employee", default=_compute_employee_id)
Avatar
Discard
Best Answer

Hello,

@api.model
def default_get(self):
    res=super(Employee,self).default_get('employee_id')
    emp_id = self.env['hr.employee'].search([('user_id', '=', self._uid)], limit=1)
    res.update({'emp_id': emp_id and emp_id.id or False})
    return res
Avatar
Discard
Best Answer

Hi,

You can try something like this,


employee_id = fields.Many2one('hr.employee', string="Employee", default=_get_employee_id)

Then,

def _get_employee_id(self):
# assigning the related employee of the logged in user
employee_rec = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)
return employee_rec.id

Thanks

Avatar
Discard