跳至内容
菜单
此问题已终结
2 回复
1873 查看

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.




形象
丢弃
最佳答案

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

形象
丢弃
最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
2
12月 23
15645
0
10月 23
33
3
10月 23
790
1
10月 23
569
1
8月 23
2958