Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
1879 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
2
dic 23
15651
0
ott 23
33
3
ott 23
790
1
ott 23
569
1
ago 23
2959