Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
1857 Näkymät

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
Hylkää
Paras vastaus

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
Hylkää
Paras vastaus

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
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
2
jouluk. 23
15628
0
lokak. 23
33
3
lokak. 23
790
1
lokak. 23
569
1
elok. 23
2952