Odoo Help

0

Attachment inside a form

By
medmars
on 10/12/17, 10:16 AM 140 views

Hi,

can we put attachment button inside a form.


Regards

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?

medmars
on 10/18/17, 5:06 AM
0
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"
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 :



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.

Register

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)

Stats

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