This question has been flagged
1 Reply
178 Views

Hello everyone 
i have odoo16

and encountered this problem

_description = 'tracking the path of employee in our enterprise'
_inherit = 'hr.employee'

contract_ids = fields.One2many('hr.contract', 'employee_id', string='Employee Contracts',
compute="_compute_contracts")
jobs_id = fields.Many2one(string='the job', related='contract_ids.job_id')

i want to retrieve the jobs objects as field what should i specify the field type should i use to get the jobs_id entities stored in jobs_id

Avatar
Discard
Best Answer

Hello Ayoub Razali,

You have taken jobs_id as Many2one field so you can store only one Job ID. Instead of related use same compute=_compute_contracts which used for contract_ids

You can add this code in _compute_contracts method of contract_ids field.

jobs_id = contract_ids[0].job_id.id if contract_ids and contract_ids[0].job_id else False

If want to store all Job ids of all contracts change type with Many2many field and use same compute=_compute_contracts which used for contract_ids

jobs_id = [(4, contract.job_id.id if contract.job_id) for contract_id in contract_ids] 


Regards,

Hemangi


Avatar
Discard
Author

thank you so much
but i had to initialize the jobs_id = contract_ids[0].job_id.id
def _get_default_contract(self):
jobs_id = contract_ids[0].job_id
default= _get_default_contract