Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
1871 Tampilan

hi,

I want to add a field in the hr. contract module called notification_id that will be calculated automatically according to the contract types.

here's my code:


class ContractNotificationInherit(models.Model):
_inherit = 'hr.contract'

notification_id = fields.Many2one('hr.contract.notification', string="renew notification for contract",
required=True,
compute="set_notification_group")

@api.multi
@api.depends
('type_id')
@api.model
def set_notification_group(self):

for rec in self.env['hr.contract'].search([]):
if rec == self.env['hr.contract.type'].search([("name", '=', 'Employee')]):
rec.notification_id = self.env['hr.contract.notification'].search([('name', '=', 'month')])

thank you in advance for any helps.




Avatar
Buang
Jawaban Terbai

You can use the below code for field compute method:

@api.depends('type_id')
def set_notification_group(self):
for rec in self:
if rec.type_id.name == "Employee":
rec.notification_id = self.env['hr.contract.notification'].search([('name', '=', 'month')], limit=1).id

Avatar
Buang
Jawaban Terbai

Hi Jenan,

You can get the required notification record using the search method by applying the appropriate domain.Since you added compute method for the notification_id field you should iterate the self since there is a possibility of getting multiple objects in self.Then search the corresponding notification for each object in self and assign the value.Also you should assign a value for each record otherwise the compute method will raise an error.So initially you should assign the False value for every record.When you are searching the notification using search method by applying the particular domain there is a chance of getting multiple records, so you should limit the result by one.

Eg:

@api.depends('type_id')
def set_notification_group(self):
   for rec in self:
    rec.notification_id = False
       if rec.type_id.name == "Employee":
           notification = self.env['hr.contract.notification'].search([('name', '=', 'month')], limit=1).id
rec.notification_id = notification.id

Regards

Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
2
Des 23
15643
0
Okt 23
33
3
Okt 23
790
1
Okt 23
569
1
Agu 23
2958