I want to create an one2one relation between two objects (hr_recruitment_stage and hr_job).
Any suggestion?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Księgowość
- Zapasy
- PoS
- Project
- MRP
To pytanie dostało ostrzeżenie
Hi,
Odoo ORM doesn’t support One2one fields, but you can do them manually. In the example below we make one2one relationship between models hr.recruitment.stage and hr.job.
In short, you set normal Many2one field (stage_id in the example) in a one model (doesn’t really matter which of the models you choose) and corresponding One2many field (stage_ids in the example) in another model. Then we add virtual Many2one field (job_id in the example) with attributes compute and inverse.
class HrRecruitmentStage(models.Model):
_name = 'hr.recruitment.stage'
job_id = fields.Many2one('hr.job', compute='compute_stage', inverse='stage_inverse')
stage_ids = fields.One2many('hr.job', 'stage_id')
@api.depends('stage_ids')
def compute_stage(self):
if len(self.stage_ids) > 0:
self.job_id = self.stage_ids[0]
def stage_inverse(self):
if len(self.stage_ids) > 0:
# delete previous reference
stage = self.env['hr.job'].browse(self.stage_ids[0].id)
asset.stage_id = False
# set new reference
self.job_id.stage_id = self
class HrJob(models.Model):
_name = 'hr.job'
stage_id = fields.Many2one('hr.recruitment.stage', string='Stage')
Hope it helps
Thank you for your help, can you also implement this using odoo online studio?
Stumbling upon this many years later, but I think perhaps an easier way is to use _sql_constraint, this should enforce a one to one relationship in the database.
So you could use:
_sql_constraints = [('make some_id one2one', 'unique(some_id)', 'some_id must be unique to ensure one2one relationship')]
some_id = fields.Many2one(...)
Podoba Ci się ta dyskusja? Dołącz do niej!
Stwórz konto dzisiaj, aby cieszyć się ekskluzywnymi funkcjami i wchodzić w interakcje z naszą wspaniałą społecznością!
Zarejestruj sięPowiązane posty | Odpowiedzi | Widoki | Czynność | |
---|---|---|---|---|
|
2
kwi 25
|
452 | ||
|
1
kwi 25
|
354 | ||
|
2
lut 24
|
1478 | ||
|
1
lip 23
|
1713 | ||
|
2
cze 23
|
2375 |