コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
1878 ビュー

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
15651
0
10月 23
33
3
10月 23
790
1
10月 23
569
1
8月 23
2959