Skip to Content
Menu
This question has been flagged
2 Replies
10421 Views

Hi,

can we put attachment button inside a form.


Regards

Avatar
Discard

If you want to attach attachment define a field like this,

attachement_ids = fields.One2many('ir.attachment', string="Attachment")

Author

It give me an error message, should i import something at the top of the model?

Best Answer

Hello medmars,

 

For example , If you want to put attachment button inside a form then you should follow below steps:

Python code :

class Employee(models.Model):
    _inherit = "hr.employee"
    _description = "Employee"
 
    doc_count = fields.Integer(compute='_compute_attached_docs_count', string="Number of documents attached")
   
    def attachment_tree_view(self):
        domain = ['&', ('res_model', '=', 'hr.employee'), ('res_id', 'in', self.ids)]
        res_id = self.ids and self.ids[0] or False
        return {
            'name': _('Attachments'),
            'domain': domain,
            'res_model': 'ir.attachment',
            'type': 'ir.actions.act_window',
            'view_id': False,
            'view_mode': 'kanban,tree,form',
            'view_type': 'form',
            'help': _('''<p class="oe_view_nocontent_create">
                                                Attach documents of your employee.</p>'''),
            'limit': 80,
            'context': "{'default_res_model': '%s','default_res_id': %d}" % (self._name, res_id)
        }
 
    def _compute_attached_docs_count(self):
        Attachment = self.env['ir.attachment']
        for employee in self:
            employee.doc_count = Attachment.search_count([('res_model', '=', 'hr.employee'), ('res_id', '=', employee.id)])

 

XML Code:

<?xml version="1.0"
encoding="utf-8"?>
<odoo>
    <data>
        <record id="view_employee_documents_form" model="ir.ui.view">
            <field name="name">hr.employee.documents.form</field>
            <field name="model">hr.employee</field>
            <field name="inherit_id" ref="hr.view_employee_form"/>
            <field name="arch" type="xml">
                <xpath expr="//button[@name='toggle_active']" position="before">
                    <button  class="oe_stat_button" name="attachment_tree_view"  type="object" icon="fa-files-o">
                        <field string="Documents" name="doc_count" widget="statinfo"/>
                    </button>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

Output :

You can see attachment button in view as below :



Avatar
Discard