Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
23829 Lượt xem

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?


Ảnh đại diện
Huỷ bỏ

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

Tác giả Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 4 25
1353
2
thg 3 25
1545
4
thg 11 24
7229
1
thg 3 24
2149
3
thg 9 23
24992