Odoo Help


This community 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.


Upload a file by code thanks to ir.attachment

Fabien REMY
on 5/6/15, 11:21 AM 2,937 views

Hi all,

I try to upload a file generated on the server by an OpenERP (v 7.0) cron task via the ir.attachment object.

 When I set the parameters passed to the ir.attachment create method, I don't know what kind of object I need for the datas parameter ?

Do I need an absolute path to my file ? An instance of my opened file ? something else ?

The error I have is : "Cannot determine mime type... I guess I don't give the right type of object.

Example :

document_vals = {'name': name_file_export_final,                        -> filename.csv

                             'datas': fname,                                                  -> path to my file (under Windows)

                             'datas_fname': name_file_export_final,            -> filename.csv

                             'res_model': self._name,                                   -> My object_model

                             'res_id': gm_record_id,                                     -> the id linked to the attachment.

                             'type': 'binary' }

ir_id = ir_attachement_obj.create(cr, uid, document_vals, context)

The record is created within the table ir_attachment, I can see the attached file on my object on the web application, but when I download the file it is stangely encoded.

My developments are done under windows, but the server is under Unix (if it could be usefull for my problem)

Thank you for your answers.


You mean file names are encoded, like 68bb912c1711dade5df906449f0d5dd3 ?

on 5/6/15, 11:26 AM

that's filename as it stored in the odoo filestore. it avoids duplicated files in filestore. the real filenames are stored in the database, for download these files with correct filename, you've to manage to have all parameters set correctly ('datas_fname' should set to real filename, etc.)

on 5/6/15, 11:33 AM

Hi Temur !! Thank you very much for your reactivity and your great answer. Now it works. Once the file read in a local variable and encoded in base64, the file is stored with its data. Thanks again. Fabien.

Fabien REMY
on 5/7/15, 3:46 AM

Il n'y à pas de quoi

on 5/7/15, 4:26 AM

Temur, One more question, regarding the mime type of my file. The module openerp.addons.document.content_index send an exception saying that it "Cannot determine mime type." Any idea ? Cheers.

Fabien REMY
on 5/7/15, 8:21 AM

try to add one more parameter (for csv file):

'mimetype': 'text/csv',
on 5/8/15, 3:24 AM

You mean add that parameter when I create the ir.attachment object within the params dictionnary ? I've already try that, but it replies me that mimetype is not an attribute of that object.

Fabien REMY
on 5/11/15, 7:47 AM

Here is the full stacktrace : 2015-05-11 13:48:13,480 5076 ERROR db_name openerp.addons.document.content_index: Cannot determine mime type. Traceback (most recent call last): File "D:\workspace_luna\openerp_server\openerp-7\openerp\addons\document\content_index.py", line 167, in doIndex pop = Popen(['file','-b','--mime',fname], shell=False, stdout=PIPE) File "D:\Python27\lib\subprocess.py", line 679, in __init__ errread, errwrite) File "D:\Python27\lib\subprocess.py", line 896, in _execute_child startupinfo) File "D:\Program Files\eclipse_luna\plugins\org.python.pydev_3.7.0.201408261926\pysrc\pydev_monkey.py", line 289, in new_CreateProcess return getattr(_subprocess, original_name)(appName, patch_arg_str_win(commandLine), *args) And a final error from Windows : WindowsError: [Error 2] Le fichier spécifié est introuvable = The specified file is not found

Fabien REMY
on 5/11/15, 7:50 AM

yes I meant to add this parameter when you create ir.attachment. agree, in v7 there is no mimetype field. I'm sorry, I actually work on v8 and in v8 there is mimetype field in ir.attachment model added somewhere. have not checked it back in v7. so 'mimetype' parameter is not a solution.

on 5/11/15, 8:34 AM

have you listed "document" module as dependency in __openerp__.py for your module? using 'depends' property?

on 5/11/15, 8:36 AM

custom indexer for csv fiels:

from document.content_index import indexer, cntIndex
class CsvIndex(indexer):
    def _getMimeTypes(self):
        return ['text/csv','application/csv','text/*']

    def _getExtensions(self):
        return ['.csv']

    def _doIndexContent(self, content):
        return content

your error comes from contentIndex.doIndex() function. it seems there is not support for indexing of csv files. you'll have to add support for it or install some module (if any) that adds such support. for the start you can try to add above custom indexer in your code. hope this'll work, if not, then you'll have to add support for indexing of csv files somehow if you want to disappear this error.
on 5/11/15, 9:11 AM

Temur, ... you rock ! It works fine now. Thank you for your precious help.

Fabien REMY
on 5/12/15, 10:41 AM

You're welcome

on 5/13/15, 5:43 AM


| 6 4 7
Tbilisi, Georgia
On 5/6/15, 12:02 PM

For datas parameter you'll need file content (encoded with base64).

Read the whole file content into the some varialbe, lets say _content, and then use as:

'datas': _content.encode('base64'),


updated with .encode('base64'), as you read file content locally at the server side, it's normal content, whereas while sending over network content is encoded with base64 encoding. so after reading normal file content into the '_content' variable, you'll need to encode it witn .encode('base64') to get base64 encoded content string instead of normal file content string.

on 5/6/15, 12:31 PM
Fabien REMY
On 5/6/15, 11:34 AM

Hi Temur,

No, the filename is encoded in a good way, just the file content (a "stupid" CSV file).

The fields "datas_fname" and "res_name" are OK. But the field file_size is not right (21 instead of 652 when I push it thanks to the graphical interface)

My question is, do I set the good type of object in the parameter "datas" ?

Fabien REMY
On 5/6/15, 11:34 AM

Hi Temur,

No, the filename is encoded in a good way, just the file content (a "stupid" CSV file).

The fields "datas_fname" and "res_name" are OK. But the field file_size is not right (21 instead of 652 when I push it thanks to the graphical interface)

My question is, do I set the good type of object in the parameter "datas" ?

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)


Asked: 5/6/15, 11:21 AM
Seen: 2937 times
Last updated: 5/13/15, 5:43 AM