Skip to Content
Menu
This question has been flagged

Hello Guys!!!

I have a computed field One2Many.

I have defined it like this:

attachment_ids = fields.One2many('ir.attachment', 'attachment_case_id', string="Attachement", compute=_computed_field)

And the function is:

def _compute_attachment_case(self):

  for attachment in self :

   mail_ids = self.env['callim.mail'].search([('dossier_id','=',attachment.id)])

   if mail_ids :

    for mail in mail_ids :

     attachment_ids = self.env['ir.attachment'].search([('res_id','=', mail.id),('res_model','=','callim.mail')])

     current_case_id = self.id

     current_case_type = self.type_id.id

     if attachment_ids :

      for attach in attachment_ids[-1:]:

       attachment.write({'attachment_ids': [(0,0, {

         'attachment_case_id' : current_case_id,

         'datas' : attach.datas,

         'description' : attach.description,

          'type_id' : attach.type_id.id,

          'type_case' : current_case_type,

       })]})

The function is called and functionning well but the result does not Write in the field!!! Even if i tried to add it as default values :/

Can anyone help please. Need Help please 

Avatar
Discard
Best Answer

Hi,

Please see this sample for the compute function of one2many in the hr recruitment module.

document_ids = fields.One2many('ir.attachment', compute='_compute_document_ids', string="Applications")

Compute function,

def _compute_document_ids(self):
applicants = self.mapped('application_ids').filtered(lambda self: not self.emp_id)
app_to_job = dict((applicant.id, applicant.job_id.id) for applicant in applicants)
attachments = self.env['ir.attachment'].search([
'|',
'&', ('res_model', '=', 'hr.job'), ('res_id', 'in', self.ids),
'&', ('res_model', '=', 'hr.applicant'), ('res_id', 'in', applicants.ids)])
result = dict.fromkeys(self.ids, self.env['ir.attachment'])
for attachment in attachments:
if attachment.res_model == 'hr.applicant':
result[app_to_job[attachment.res_id]] |= attachment
else:
result[attachment.res_id] |= attachment

for job in self:
job.document_ids = result[job.id]
job.documents_count = len(job.document_ids)

Thanks

Avatar
Discard
Author

Hi my friend!! Thanks a lot for the example. It s well created but i want to correct my function why it desnt work would you please help :/

Author

no one can help here??!!

Related Posts Replies Views Activity
1
Sep 22
1010
4
May 18
9450
0
Sep 24
108
0
Mar 23
3773
1
Jul 20
6753