This question has been flagged
3 Replies
11495 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!

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

Author

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!

Author

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

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?

Author

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.

Author Best Answer

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!

Avatar
Discard
Best Answer

Hi Fahreza,

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

Thanks!

Avatar
Discard
Author

^ I edited my question. Thanks