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
22980 Widoki

I am using Odoo 13 and I have a file stored in a fields.Binary with attachment=True (Odoo's default value). How do I obtain the url to that file given a record? I want to store the url to a computed fields.Html so that I can display it on my form view.
This is the code I have at the moment in my py file (models):

class Image(models.Model):
    def _get_image_html(self):
        for elem in self:
            image_url = self.env['ir.attachment'].search([('res_id', '=', str(self.image))], limit=1).url
            elem.image_loc = '<img src="%s"/>' % image_url

    image = fields.Binary(string="Image")
    image_loc = fields.Html(string='Preview 2', compute="_get_image_html")


So far, my code has only been returning a link to /False.

Any help with be much appreciated.

Awatar
Odrzuć
Najlepsza odpowiedź

Odoo does not store the URL for the attachments stored at the physical location (filestore).
But there is a way to create a URL to show the image in the report/website based on the attachment record.

Ex:

attachment = self.env['ir.attachment'].search([('res_model', '=', 'my.custom_model'), ('res_id', '=', self.id)], limit=1)

img_url = "/web/image/ir.attachment/%s/datas" % attachment.id

# For website/report
<img t-att-src="/web/image/ir.attachment/attachment.id/datas"/>
Awatar
Odrzuć
Autor

Thank you for the response. However, I do not know why but it keeps returning False: 127.0.0.1:8069/web/image/ir.attachment/False/datas

This is what my actual model looks like:

class Image(models.Model):

_name = 'gov_bene_phils.sss_image'

def _get_image_html(self):

for elem in self:

attachment = self.env['ir.attachment'].search(

[('res_model', '=', 'gov_bene_phils.sss_image'),

('res_id', '=', self.id)], limit=1

)

image_url = "/web/image/ir.attachment/%s/datas" % attachment.id

image = fields.Binary(string="Image")

image_loc = fields.Html(string='Preview 2', compute="_get_image_html")

In my xml file, I just place it in my form view with <field name="image_loc" />

* I have other attributes for the gov_bene_phils.sss_image model but they do not have anything to do with the document being attached. Any thoughts on what's wrong with my code?

Najlepsza odpowiedź

If your aim is to dispaly image on view, then there is no need to declare other field(image_loc) to privew it 

in model

    image = fields.Binary(string="Image")

in view(XML) define it with widget="image" like

<field name="image" widget="image"/>


Awatar
Odrzuć
Autor

That works for images and I have tried that. However, I also want to embed a pdf as well. I will eventually change the img tag to embed to make it work. I also tried the following code:

self.env['ir.attachment'].search(

[('res_model', '=', 'my.custom_model'),

('res_id', '=', self.id)], limit=1

).url

It keeps returning http://127.0.0.1:8069/False

I'm not sure how to manipulate the ir.attachment model properly.

Powiązane posty Odpowiedzi Widoki Czynność
0
mar 25
429
2
lip 22
11479
2
lip 23
3130
1
lip 22
19568
2
wrz 21
7861