Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
25844 Widoki

hello .. this is the 3rd time to ask my question ...


i have some images stored in file store .. i want to display them in my form view so how can i do it ?

is there anyway to use image url to display it or not ?
or if any another way



Thank you ~!

Awatar
Odrzuć

H Ahmed, sorry for the delay in responses, please provide more info for a proper answer, there are some ways to get it done

Najlepsza odpowiedź
Hi Ahmed
If your record have a relation Many2one field with the attachment you could use a related field to display the datas binary computed field
if not and you are still able to get the attachment id somehow you could build the url for the image using this format
/web/content/<int:id>
replacing <int:id> with the attachment id, that url need to be used in an img tag src to be able to properly show the image. You could display it using one of this 2 ways:

1- Overriding the method fields_view_get in you model to set the src attr of the img tag by using xpath and python code (the hard way). For example put this in your form
<img class="img-url-set"/>
And usign xpath locate that node in the form to set the attribute src with the url value

2- By using an HTML calculated field that contains the dynamic html code for the src of the img tag. For example
define a field html that build the whole html for the image like:
img_attach = fields.Html('Image', compute="_get_img_html")

def _get_img_html(self):
    for elem in self:
        img_url = '/web/content/%s' % attachment_id
        elem.img_attach = '<img src="%s"/>' % img_url
Just add that field img_attach in the form and set it as readonly to be able to display your image
Hope this helps
Awatar
Odrzuć

I have been trying to follow along but I do not know how to obtain my attachment_id.. I am using Odoo 13 and my attachment is stored in a fields.Binary with attachment=True. How do I get the img_url?

You could use the same domain that Odoo use to locate the attachment associated with a Binary field with attachment=True

domain = [

('res_model', '=', record._name),

('res_field', '=', field_name),

('res_id', '=', record.id),

]

attach_id = self.env['ir.attachment'].sudo().search(domain)

Najlepsza odpowiedź

You can use a python script to read a file and store it in ir_attachments and display in your form or whatever you need. you can also use custom models by inheriting ir_attachments.

Awatar
Odrzuć
Najlepsza odpowiedź

use our module it has code and guideline about it https://apps.odoo.com/apps/modules/13.0/ir_attachement_customization/

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
cze 23
4312
2
lip 22
11073
1
sie 20
7111
2
lip 23
2837
2
wrz 21
7633