In this post you will learned about binary fields in Odoo. You will also learned how to create and use binary fields or save images in Odoo form view or odoo reports like QWEB reports. 

What are Binary Fields in Odoo

       Binary Fields Stores file, encoded in base64 in bytea column in your created table. For example I have a table and the last column of the table shows binary data.

In the below screen shot you can see that column.

How to Create Image or File Upload (Binary) Field and Set Uploaded file name as the Orignal File on Download

 

How to create Binary Fields

If you want to create a binary field in existing model than you need to inherit already existing model. 


If you are going to use your new model than follow below steps to create file upload field (binary).

Creating File Upload (Binary) Field in Odoo

As you can see that I have created a model and two fields. One of the field is type Binary called "image", which will be used to upload images. And the other one is a Char type field named "image_file_name", the purpose of this field is to store the orignal file name in the database which we are going to upload. 

Now we will create form view to show the above created binary field.


The above code will create a form view which shows our created binary field to upload images or files. Below screen shot shows how created binary field looks like in from view.

How to Create Image or File Upload (Binary) Field and Set Uploaded file name as the Orignal File on Download

Now select an Image. This stored image file encoded in base64 in bytea column in your created table. 

How to show uploaded images in Qweb Reprots

        Once the images has been uploaded its time to show in qweb reports. For that purpose you have to follow these stpes. The below code show the xml file for creating qweb reports in odoo. In this file we will call our image field which one is uploaded from form view of odoo.

The below line of code is the key part of showing binary field (image) in qweb reports or your template.

<img t-attf-src="data:image/jpg;base64,{{ your_model_name.binary_field_name }}" style="width:95px; height:95px;margin-left:55px;"/>

Here "your_model_name" is the name of your model or object for example "(my.model(4,))" , and "binary_field_name" is the name of the "Binary Field" created in odoo model. In our case its "my_images".

The above code will print the images in the original format, we can change the size of image simply applying by some css. You simply need to call the "t-attf-src" and you can print them out.