Search code examples
moduleselfodoo-13uid

Odoo 13: Check if a user has only a single related employee


I need to check if certain user has one and only one employee associated.

So far, I have this:

def _employee_get(obj, cr, uid, context=None):
    ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
    if ids:
        if len(ids) > 1:
            return False
        else:
            return ids[0]
    else:
        return False

class History(models.Model):
    _name = 'hospital.history'
    _description = 'hospital.history'

    date = fields.Date(required=True, default=fields.Date.today(), readonly=True)
    name = fields.Many2one('res.partner', string="Patient", required=True)
    healthworker = fields.Many2one('hr.employee', string='Sanitarian', default=_employee_get, readonly=True)
    testresults = fields.Text(string="Tests' Results")
    description = fields.Text(string="Description", required=True)
    specialities_ids = fields.Many2many('hospital.speciality', 'hospital_speciality_history_rel', 'history_id', 'speciality_id', 'Speciality')
    status = fields.Selection([('admitted','Admitted'),('outpatient','Outpatient Clinic')], 'Status', required=True)

But when I call the function I get this error:

TypeError: _employee_get() missing 2 required positional arguments: 'cr' and 'uid'

Have tried several different ways, but can't get it to work so any help will be much appreciated.


Solution

  • alfa,

    You have performed the old code in the new version odoo(13).

    This _employee_get method should be like this way :

    def _employee_get(self):
        return self.env['hr.employee'].search([('user_id', '=', uid)], limit=1)