'Odoo 14, How to get url of file uploaded or generated

I did upload files on Odoo using backend(dashboard) and I want to return url in api to enable user to download it from mobile app



Solution 1:[1]

I'm assuming your referring to the Attachment module? If so you can inherit the 'ir.attachment' module and create a new computed field to display the public url.

class IrAttachment(models.Model):
    _inherit = 'ir.attachment'

    public_url = fields.Char('Public Image', compute='_create_url', help='This is the link for the image or file')

    def _create_url(self):
        for record in self:
            base_url = self.env['ir.config_parameter'].get_param('web.base.url')
            record.public_url = base_url + '/web/content/' + str(record.id) + '/example.png'

Then make sure you add your new field to the form view.

 <record id="custom_attachment_form" model="ir.ui.view">
        <field name="name">attachment.custom</field>
        <field name="model">ir.attachment</field>
        <field name="inherit_id" ref="base.view_attachment_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='mimetype']" position="after">
                    <field name="public_url" widget="url"/> 
            </xpath>
        </field>
    </record>

You can now call this field using the Odoo api also.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scott