Help

0

How to save a attached file in odoo 10?

Avatar
Silviaa

Hi,

I am working on odoo 10, I need to attach a documents by clicking a button in tree view on Vendor Bills menu. I have created a button in the tree view for all the rows, when clicking a button i have to popup a form which contains upload button.

When click this upload button a popup shows to select a file to upload. This works perfectly for me.

After upload i have to save the attached document for whenever the user wants to open the document, the uploaded file will be always shows in the popup form.

In this i uploaded the document after i open the form it doesn't shows the uploaded form.Can anyone help me to solve this issue

My Code,

Show popup:

def attach_documents(self):

            return {

                'type': 'ir.actions.act_window',

                'view_type': 'form',

                'view_mode': 'form',

                'res_model': 'upload.file',

                'create': False,

                'target': 'new',

            }    


Upload File:

attachment_ids = fields.Many2many(

        'ir.attachment',

        string='Attachments',

        help='Attachments are linked to a document through model / res_id and to the message '

             'through this field.')


Avatar
Discard
2 Answers
0
Avatar
Silviaa
Best Answer


Avatar
Discard
0
Avatar
IT Libertas
Best Answer

Hi,

as I understand, you would like to store attachments to an invoice, and when click on a button to show attachments of this invoice?

If so, follow the plan:

1. Add to account.invoice the field, attachment_ids as m2m to ir.attachment

2. Add to popup a reference to invoice, for example, the field invoice_id

3. In returning popup add context, like

context: "{'default_attachment_ids': [(6,0, self.attachment_ids.ids)], 'default_invoice_id': self.id}"

It would automatically add existing files to your popup and link popup to an existing invoice

4. Then add to your popup some button to store added attachments. E.g.:

In XML:
<button name="save_action" type="object" string="Save" class="oe_highlight"/>
In Python:
@api.multi
def save_action(self):
     for object in self:
         object.invoice_id.attachment_ids = [(6,0,object.attachment_ids.ids)]
2 Comments
Avatar
Discard
Avatar
Silviaa
-

For attach document, I got a new issue in this

when i click a button it always shows "form view could not be loaded"

This is my code to open a popup form

class AccountInvoice(models.Model):

_inherit = "account.invoice"

def attach_documents(self):

res_id = self.id

return {

'name': _('Upload Documents'),

'type': 'ir.actions.act_window',

'view_type': 'form',

'view_mode': 'form',

'res_model': 'upload.file',

'res_id': res_id,

'create': False,

'target': 'new',

'context': {'default_invoice_id': self.id}

}

XML:

<record id="invoice_supplier_tree_inherit" model="ir.ui.view">

<field name="name">account.invoice.supplier.tree.inherit</field>

<field name="model">account.invoice</field>

<field name="inherit_id" ref="account.invoice_supplier_tree" />

<field name="arch" type="xml">

<xpath expr="field[@name='state']" position="after">

<button name="attach_documents" string="Attach" type="object"

icon="fa fa-paperclip" class="fa-paperclip" />

</xpath>

</field>

</record>

and to save the document

class UploadFile(models.Model):

_name = "upload.file"

attachment_ids = fields.Many2many(

'ir.attachment','id',

string='Attachments',

help='Attachments are linked to a document through model / res_id')

@api.multi

def save_action(self):

for line in self:

line.attachment_ids = [(6, 0, line.attachment_ids.ids)]

XML:

<record model="ir.ui.view" id="upload_file_form">

<field name="name">upload.file.form</field>

<field name="model">upload.file</field>

<field name="arch" type="xml">

<form create="false" string="Templates">

<field name="attachment_ids" widget="many2many_binary" />

<!-- <button name="save_action" type="object" string="Save" class="oe_highlight"/> -->

<footer>

<button name="save_action" class="btn-primary" type="object" string="Upload" default_focus="1"/>

<button string="Cancel" class="btn-default" special="cancel" />

</footer>

</form>

</field>

</record>

if i give the "res_id" it does not open the popup form, if i remove 'res_id' it will open the popup but after i attach and upload the file it does not display the attached files in the popup

What shoud i do, to open the form in popup and save the attachment in popup..

Avatar
Silviaa
-

I found the solution for this..

Thank you..