Skip to Content
Menu
This question has been flagged

Hey everyone,

I got two issues related to the res_model and res_id fields in ir.attachment.


1) In a custom module I am trying to add attachtments to a HTML field. However res_id in ir.attachments remains at value 0 for all tickets  that I post.

For the attachments I copied the approach from the project module:


class Ticket(models.Model):

attachment_ids = fields.One2many('ir.attachment', compute='_compute_attachment_ids', string="Attachments", help="Attachment that don't come from message.")

def _compute_attachment_ids(self):
for ticket in self:
attachment_ids = self.env['ir.attachment'].search([('res_id', '=', ticket.id), ('res_model', '=', 'website.ticket')]).ids
message_attachment_ids = ticket.mapped('message_ids.attachment_ids').ids # from mail_thread
ticket.attachment_ids = list(set(attachment_ids) - set(message_attachment_ids))

But as res_id is not set correctly I do not get the correct number of attachments. When posting using mail.thread's Notes or document's Attach Files button res_id is set correctly.


2) The form view comes with additional TransientModels to send emails and post to the mail.thread. However I want  all attachments, including  mail.thread  posts of  those  TransientModels,  to  show  up  in  the  Attach  Files  drop  menu  of the documents module. For this, I guess I need to set res_model to the main model and res_id to the TransientModels parent id.

E.g.:

class Ticket(models.Model):

_name = "ticket"

ticket-id = 3


class Ticket.Forward (models.TransientModels):

_name = "ticket.forward"

id = ticket-id = 3


Sadly I have no idea how to accomplish this. Help would be greatly appreciated.

Avatar
Discard

Transient models cannot be used to store permanent data objects if you want that data you should use regular models

Author Best Answer

 The TreansientModels create emails that are stored in the mail.thread. Attachments of those are stored in ir.attachments but do contain 'res_model' of the transientModel not to underlying Model and do not store the res_id of the underlying model.

E.g.   

class  Ticket(models.Model):
_name  =  "website.ticket"

Stores res_model "website.ticket" and should store the ticket-id in res_id

class  TicketCompose(models.TransientModel):
    _name  =  "website.ticket.compose"

stores "website.ticket.compose"  in  res_model  and  0  in  res_id.

I want to add all those attachments (from description in the form view and from the mail.thread) to the 'Attach Files' button of the document model.

To my understanding for this all attachments of each ticket require the value res_model= "website.ticket" and res_id="ticket_id" in the ir.attachment model.

            


Avatar
Discard
Related Posts Replies Views Activity
0
Jun 22
2186
0
Feb 16
3080
1
Mar 24
729
0
Nov 23
495
0
Feb 23
1208