Odoo Help


Attachment inside a form

on 10/12/17, 10:16 AM 334 views


can we put attachment button inside a form.


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

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

Niyas Raphy
on 10/12/17, 10:57 AM

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

on 10/18/17, 5:06 AM

Jainesh Shah

--Jainesh Shah--
| 5 2 7
Ahmedabad, India
--Jainesh Shah--

Jainesh Shah
On 10/14/17, 5:34 AM

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"
        <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"/>

Output :

You can see attachment button in view as below :

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 10/12/17, 10:16 AM
Seen: 334 times
Last updated: 10/14/17, 5:34 AM