Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
11437 Vizualizări

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 

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

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 :/

Autor

no one can help here??!!

Related Posts Răspunsuri Vizualizări Activitate
1
sept. 22
2188
4
mai 18
11119
0
sept. 24
1246
0
mar. 23
5151
1
iul. 20
8493