Odoo custom form field widget - how to display field value?

I created a custom widget in Odoo and displayed it for the form field. My template looks like this:

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

How can I put a field value in an <img> tag src attribute?

+6
source share
2 answers

After spending a day digging around in the source code, I found a solution! This is actually not related to the template, but I got the idea from the source text widget source code by default, so I think that this should not be considered as a “hack”.

Here is 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 contains nothing special:

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

It works like a charm. It even refreshes the page whenever I make changes on the server side.

The Odoo documentation really needs to be more talkative.

Update: The answer applies to Odoo 8. It may work slightly differently in Odoo 9 because they redefined the user interface structure in the new version.

+8
source

We can do this:

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

Where

model.name is the name of the table,

image_small is the name of the field that will store / store the binary data type.

EDIT:

To display the field value in the template, you can try with this

 <img t-att-src="record.field_name"/> 
+1
source

All Articles