Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
11526 Lượt xem

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 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Tác giả

no one can help here??!!

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 9 22
2254
4
thg 5 18
11182
0
thg 9 24
1283
0
thg 3 23
5241
1
thg 7 20
8531