This question has been flagged
3 Replies
19669 Views

Hi,

I have a custom image uploaded from user and it is stored in ir_attachment table, now i want to show this image in qweb report. How can i do this?


Avatar
Discard

<img t-attf-src="data:image/jpg;base64,{{ your_image_field }}" style="align:left; width:85px;height:85px;"/>

Author Best Answer

Actually i have defined a custom binary field to upload a image and the field was not showing up as column in the database and upon searching i found out that the attachments i.e., binary fields gets stored in ir_attachment table and i looked for my field in this table and found it there as a column. Now the challenge for me was to retrieve it and display as a image in qweb report. I tried multiple things and had no luck and i was finally successful by trial and error method. What i did was, I wrote a simple py function which will accept id number and then i'm searching the model where my field was defined, then fetching my desired record using self.env[].search and then returning the actual field where the image was uploaded.

def get_logo(self,id_no):
    logo = (self.env["account.analytic.account"].search([("id", "=", id_no)])).inv_logo
    return logo

Then in the xml

<t t-if="o.company_selection" t-set="company_logo" t-value="o.get_logo(o.company_selection)"/>
<img t-if="company.logo" t-att-src="image_data_uri(company_logo)" style="max-height: 45px;"
alt="Logo"/>

Here company_selection is the id of the field, where i uploaded the image..I'm then passing it to py function and i get the field as return value and i'm storing it in company_logo and then i'm passing the field company_logo into the image_data_uri and displaying it under the image tag and it worked flawlessly.

Avatar
Discard
Best Answer

Hello,

Check below code may be it's help you

<img t-if="object.field_name" t-att-src="'data:image/png;base64,%s' % object.field_name"/>

Thanks

Avatar
Discard
Best Answer

First, you need to retrieve the image data from ir_attachment table where reference will be res_model = 'res.users' and res_id will be current id. then you have to make it in qweb report using img tag.


<img class="image" t-att-src="'data:image/png;base64,%s' % res_company.logo" style="border:auto;"/>

here in t-att-src you can retrieve data by a function in the related model.

Avatar
Discard