Odoo Help

Welcome!

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.

2

One2Many Attachment Field Download Error

By
PT SIVI
on 12/5/13, 5:30 AM 3,805 views

I need to create some object which able to have many attachments.

image description

As the figure shows, The object has two medical attachment entitled first and second. The files are uploaded too which names are tes.csv and tes2.csv

When I check them to Knowledge->Documents->Documents Menu, files were uploaded successfully as you can see here. image description

But, the problem is, when I click to download the attachment files, for example tes.csv file from one2many tree, the browser will get .bin files not .csv files.

This makes me frustrated, because of when I tried to download them from Knowledge->Documents->Documents Menu, the browser gets .csv files successfully.

You can see the difference of two methods when I'm hovering my mouse to these two form view.

1. My Module->Form View->One2Many Field
data:application/octet-stream

image description

2. Knowledge->Documents->Documents Menu->Form View->File Content Field
javascript:void(0)

image description

When I trace them, the First Method won't call ir_attachment._file_read method. And the second Method will call ir_attachment._file_read method for sure.

Please help me, so my new one2many field module attachment can download the files with their extension not just a BIN.FILE.

Thank you

Edited :

This one is .py

_name = 'hr.medreq'
_columns = {
    'name':fields.char('Name'),
    'attach_ids':fields.one2many('ir.attachment', 'medreq_id', 'Medical Attachment'),

    'attachment_ids': fields.many2many('ir.attachment', 'class_ir_attachments_rel', 'class_id', 'attachment_id', 'Attachments'),
   }

This one is .xml

<tree string="Medical Attachment">
    <field name="name"/>
    <field name="type" invisible="1"/>
    <field name="store_fname" invisible="1"/>
    <field name="datas" filename="datas_fname"/>
    <field name="datas_fname" invisible="1" modifiers="{'invisible': true}"/>
</tree>

Thanks!

3
Nuno Marques
On 12/19/13, 8:56 AM

Hi Fahreza,

I'm in Dan's team. I managed to fix this bug by editing /addons/web/controllers/main.py file. Search the saveas_ajax function. Arround the line nr 1307 you will find this:

    if filename_field:
        fields.append(filename_field)
    if data:
        res = { field: data }
    elif id:
        res = Model.read([int(id)], fields, context)[0]

edit it to this:

    if filename_field:
        fields.append(filename_field)
    if data:
        res = { field: data }
        # FIX
        if filename_field:
            filename_name = Model.read([int(id)], [filename_field], context)
            res[filename_field] = filename_name and filename_name[0] and filename_name[0][filename_field] or ''
    elif id:
        res = Model.read([int(id)], fields, context)[0]

The problem was in that case "res" didn't have filename_field filled, so a few lines later it names the file with the model name and attach id (like ir_attachment_1) but the data is fine. If you just rename the file to the correct file extension it works fine. With that fix, it fills the filename_field so it works fine.

Hope it works for you too.

Great! It seems possible. I will try it and contact you soon. Thanks a lot, Nuno!

PT SIVI
on 12/22/13, 6:46 AM

Great! I just tried it on my local server and it works 50% now. I mean, there are two methods to open this attachment right? The first method is open the related object first in one2many fields, and after window pops out, I successfully open the attachment with right extensions by clicking on the 'File Content' field. But the second method is directly click the link on one2many field columns, in this case 'datas' field. The first method works 100% but the second method still produces 'data:application/octet-stream:64'. See hee http://goo.gl/mZ9RUQ . Thank you very much by the way!

PT SIVI
on 12/25/13, 5:33 AM

you can easily understand what I mean if you change the one2many field tree view in .xml file into this <tree string="Medical Attachment" editable="bottom">

Thank you

PT SIVI
on 12/25/13, 5:34 AM

hi Fahreza, i'm following your post. I try it in my custom module. i got problem "when you click on file to dowload. It dosenot reach the "saveas_ajax" function." Could you help me this problem, plz?

phu nguyen
on 2/17/14, 3:11 AM

Yes, actually I'm still having the same problem and I can't get it right. That's why I haven't closed this question yet.

PT SIVI
on 2/17/14, 8:03 AM
0
Dan Mendes
On 12/17/13, 10:54 AM

Hi Fahreza,

Can you show me how you did that one2many field in the _columns, and the corresponding view?

Thanks!

^ I edited my question. Thanks

PT SIVI
on 12/18/13, 2:24 PM
0
PT SIVI
On 12/18/13, 2:19 PM

This one is .py

_name = 'hr.medreq'
_columns = {
    'name':fields.char('Name'),
    'attach_ids':fields.one2many('ir.attachment', 'medreq_id', 'Medical Attachment'),

    'attachment_ids': fields.many2many('ir.attachment', 'class_ir_attachments_rel', 'class_id', 'attachment_id', 'Attachments'),
   }

This one is .xml

<tree string="Medical Attachment">
    <field name="name"/>
    <field name="type" invisible="1"/>
    <field name="store_fname" invisible="1"/>
    <field name="datas" filename="datas_fname"/>
    <field name="datas_fname" invisible="1" modifiers="{'invisible': true}"/>
</tree>

Thanks!

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

4 follower(s)

Stats

Asked: 12/5/13, 5:30 AM
Seen: 3805 times
Last updated: 3/16/15, 8:10 AM