跳至內容
選單
此問題已被標幟
1 回覆
5434 瀏覽次數

Prerequisite

I made all the test in v15 so it should work for all sub versions (15.0, 15.1 and 15.2). 

Be in debug mode.

Have an import compatible file of some attachments.

Studio.


Use case

It often happens that we want to make a mass import of attachments in Odoo (in the ir.attachment model) directly but we are stuck very fast by the fact that the Resource Model (res_model) or Resource ID (res_id) is a read only field and that it cannot be altered in a standard version. 

頭像
捨棄

You may also use this third-party app - https://apps.odoo.com/apps/modules/15.0/cloud_base - to move attachments between folders and, hence, between linked objects. It is as simple as a mere drag and drop or choosing a field in the popup wizard.

作者 最佳答案

Solution


What I propose here is to find a workaround by linking the attachments to a comment in the chatter (mail.message) where the Related Document Model (model) and Related Document ID (res_id) are editable. 

So, imagine that you have a file that is import compatible with many attachments (base64, direct link, ...) and that you want to import it and link it to a given record on a given model. This is all possible through the mail.message model and a hidden many2many field on the mail.message model. To do so, we are going to proceed in 4-5 steps (depending on how you want to divide your work flow): 

  1. Import all your attachments with the following fields: 
    1. External ID: The external ID of my attachment.
    2. Name ("name"): Name of the attachment.
    3. Type ("type"): Type of the attachment, URL or a file. Here I am going to import the attachment in a base64 format so I will select the "File" type. 
    4. File Content (base64) ("datas"): This is the content of your attachment directly. If the type is URL, we will have the "Url" field. I will focus on the former here.
  2. Export the models and record's database ID on which you will want to link the attachment to. This will be helpful to get the database ID for the following step. You can also retrieve the ID of the record from the URL, you just need to find "id=112" for instance.

  3. Import the messages on which you want to link the attachments with the following fields.
    1. External ID: The external ID of the chatter message. 
    2. Type ("type"): Here, I believe it is better to import on the "Comment" type so that your message stays internal to Odoo. It does not really matter at this point of the process but as the field is mandatory, we need to tell which type we need. 
    3. Related Document Model ("model"): This is the model on which you want to link the attachment
    4. Related Document ID ("res_id"): This is the ID (Database ID not the external ID) of the record on which you want to make the import. Just a reminder that the Database ID is unique per model and that the External ID is unique on the whole database). Note also that you will need to make an export of this as you cannot create/edit the ID. 
    5. You can add a subject (on "subject") and a body (on "body") so that the message looks cleaner.

  4. Add the many2many field pointing to ir.attachments (in the existing fields) to the mail.message model. You can do it as follow: https://watch.screencastify.com/v/0wNIkBbRsxtTEcrVItbO


  5. Import a mapping file on mail.message with the following fields: 
    1. External ID ("id"): This enables you to update the message record. 
    2. Attachments/External ID ("attachment_ids/id"): This will make the link between the ir.attachment model being the attachment and the message on which you want to make the import. 



Please find here a video on how I managed to do it. Note that I made step 1 in 2 steps as imported my content in a second step: https://watch.screencastify.com/v/xBFBq2v1SVpExbUMdnAv  


Note: Be aware that the order in which the messages (from mail.message) will be displayed is not based on the date field but on the order of the ID of the message. This means that if I have 2 messages A & B with respectively a date in 2021 and 2022 and that A has ID 5 and B ID 4, B will be displayed on the bottom and A on top. 


Please do not hesitate to reach out if you have any questions!

頭像
捨棄

I cant open your video link. I want to view the pdf without download it.

相關帖文 回覆 瀏覽次數 活動
0
3月 25
494
1
3月 25
29
0
3月 25
424
3
9月 24
3493
1
8月 24
467