This question has been flagged
2 Replies
5771 Views

Hi,

I have a button, when i click this button it  shows a form in popup. But when i click it  will shows an error "Form could not be loaded"

My Code:

In Python,

class AccountInvoice(models.Model):

    _inherit = "account.invoice"


    def attach_documents(self):

        return {

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

            'view_type': 'form',

            'view_mode': 'form',

            'res_model': 'upload.file',

            'res_id': self.id,

            'target': 'new',

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

        }

class UploadFile(models.Model):

    _name = "upload.file"

    attachment_ids = fields.Many2many(

                        'ir.attachment',

                        string='Attachments',

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


In 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>


can anyone help me to resolve this issue.

Avatar
Discard
Best Answer

change your action varsas :-

optionally you can add views as a tuple like

tree = self.env.ref('yourmodel.tree_view_id')

form = self.env.ref('yourmodel.form_view_id')

return {

            'model': 'ir.actions.act_window',

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

            'view_type': 'form',

            'view_mode': 'tree,form',

            'views': [(tree, 'tree'), (form, 'form')],

            'res_model': 'upload.file',

            'res_id': self.id,

            'target': 'new',

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

        }

Avatar
Discard
Author

Hi Hilar,

I have changed my code but it still shows an error 'Form view could not be loaded'

form = self.env.ref('skit_attachment.upload_file_form').id

return {

'model': 'ir.actions.act_window',

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

'view_type': 'form',

'view_mode': 'form',

'views': [(form, 'form')],

'res_model': 'upload.file',

'res_id': self.id,

'create': False,

'target': 'new',

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

}

view_id = self.env.ref('skit_attachment.upload_file_form').id

return {

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

'view_type': 'form',

'view_mode': 'form',

'view_id': view_id,

'res_model': 'upload.file',

'res_id': self.id,

'create': False,

'target': 'new',

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

}

Author

Hi Mohammed,

I also tried your code modifications, but still i got the same error

I dont know what to do..

In this issue if i remove the 'res_id:self.id' form will open, otherwise it shows an error..

But i need res_id because i want to display the uploaded documents for each row

Best Answer

Hi,Problem :

You're passing res_id=self.id, but self refers to the account.invoice record. However, in the action res_model='uplod.file', the res_id is from account.invoice, which doesn't match the uplod.file model. There might be a mismatch as the res_id doesn't exist in the 'upload.file' model.Solution :
You need to first create record of upload.file after you can use it in res_id. we can do like this.

class AccountInvoice(models.Model):
  _inherit = "account.invoice"  def attach_documents(self):
    upload_file = self.env['upload.file'].create({
     # your values
    })
    return {
      'type': 'ir.actions.act_window',
      'view_type': 'form',
      'view_mode': 'form',
      'res_model': 'upload.file',
      'res_id': upload_file.id,
      'target': 'new',
      'context': {'default_invoice_id': self.id}
    }

Avatar
Discard