Ir al contenido
Menú
Se marcó esta pregunta
3 Respuestas
23428 Vistas

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.

Avatar
Descartar
Mejor respuesta

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"/>
Avatar
Descartar
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?

Mejor respuesta

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"/>


Avatar
Descartar
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.

Publicaciones relacionadas Respuestas Vistas Actividad
0
mar 25
572
2
jul 22
11765
2
jul 23
3281
1
jul 22
19840
2
sept 21
8065