Odoo Help


How to Set Default Directory for Attachments Depedning on Object

Cyrus Waithaka
on 3/18/15, 5:25 PM 1,769 views

I am implementing the Knowledge Management module in odoo v8 and I have come across a major short coming.The directory field in the attachments is not automatically set by the system even if I configure the "Folders per resource" part in Knowledge.

For example, I have a Clients folder whose parent folder is documents. This folder is set as "Folders per resource" for the partner model. Now, if I attach an attachment on a customer, it is not being placed in the clients folder by default.

Does anyone know how to make attachments in odoo to go to a specific directory by default so I don't have to manually set the directory field in the attachment form. I feel this is a vital feature in as documents organisation is concerned. 

(Please upvote the question so that I can be able to comment)

Cyrus Waithaka
On 3/23/15, 3:05 AM

I found some great explanation here https://www.odoo.com/forum/help-1/question/where-are-document-attachments-stored-529 (Antony Lesuisse's answer) on how to set odoo to save the attachments in the file system.

I then used the following piece of code to set a default for 'parent_id' field in the attachment. (ir.attachment class)

from openerp.osv import osv, fields
from openerp.tools.translate import _
class ir_attachment(osv.osv):
    #extend document
    _inherit = "ir.attachment"
    def create(self, cr, uid, vals, context=None):
        if context is None:
            context = {}
        if not vals.get('res_model', False) and context.get('default_res_model', False):
            vals.get['res_model'] = context.get('default_res_model', False)        
        if not vals.get('parent_id', False):
            parent_id = self.pool.get('document.directory').search(cr, uid, [('ressource_type_id','=',vals.get('res_model'))])
            if parent_id and parent_id[0]:
               vals['parent_id'] = parent_id[0]
                vals['parent_id'] = self.pool.get('document.directory')._get_root_directory(cr,uid, context)
        return super(ir_attachment, self).create(cr, uid, vals, context)
class document_directory(osv.osv):
    _inherit = 'document.directory'
    _sql_constraints = [
        ('dir_uniq', 'unique (ressource_id,ressource_parent_type_id)',
         'The Directory name must be unique per Resource Type and Resource !'),
This atleast did what I needed.

Hello Cyrus,

The functionality "Folders by resource" only works in relation to module "document_ftp". That is, when you access to your repository using FTP, the application dynamically constructs one folder per resource (e.g. one folder for each sales order), based on the flag that you set to the 'Customers' folder in Odoo. These are not physical but 'virtual' folders.

What you ask for is interesting, but would require a specific development, so that each object in Odoo would create it's own physical folder per resource.




Unfortunately, document_ftp has not been ported to odoo v8. Please see how I achieved what I wanted in my answer below

Cyrus Waithaka
on 3/23/15, 3:02 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 3/18/15, 5:25 PM
Seen: 1769 times
Last updated: 4/28/15, 3:38 AM