6

I've created a custom widget in Odoo, and display it for a form field. My template looks like this:

<t t-name="ImageDisplayer">
    <img t-att-src="?"/>
</t>

How can I put the field's value into the <img> tag's src attribute?

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
Aron Lorincz
  • 1,677
  • 3
  • 19
  • 29

2 Answers2

8

After spending a day digging in the source code, I've found the solution! It doesn't really involve the template, but I got the idea from the source code of default text field widget, so I think it shouldn't be considered as "hacking".

Here's my custom widget class:

openerp.mymodule = function(instance, local) {
    instance.ImageDisplayer = instance.web.form.AbstractField.extend({
        template: "ImageDisplayer",
        init: function (view, code) {
            this._super(view, code);
        },
        // The key part:
        render_value: function() {
            this.$el[0].src = this.get("value");
        }
    });
    instance.web.form.widgets.add('ImageDisplayer', 'instance.ImageDisplayer');
}

My template now does not contain anything special:

<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
    <t t-name="ImageDisplayer">
        <img />
    </t>
</templates>

Works like a charm. It even updates the page whenever I do a change on server-side.

Odoo documentation should really be more talkative!!!

Update: the answer applies to Odoo 8. It may work slightly differently in Odoo 9, because they've revised the UI framework in the new version.

Aron Lorincz
  • 1,677
  • 3
  • 19
  • 29
1

We can do like that

<img t-att-src="kanban_image('model.name', 'image_small', record.id.value)"/>

Where

model.name is table name,

image_small is a field name which will hold/store binary type of data.

EDIT:

To display value of field in template, you may try with this

<img t-att-src="record.field_name"/> 
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58