Thursday, 2 June 2016

Using Binary Fields in Odoo

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:
       Binary Fields Store 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 Binary Fields:
       The first step is to create a new model and than new fields.

class campus(models.Model):
    _inherit = '')
    my_image = fields.Binary(string="Upload Logo")

As you can see that I have created a model and 1 field of type Binary called "my_image", which will be used to upload images. Now we will create form view to show the above created binary fields.

        <record model="ir.ui.view" id="unique_form_view_id">
            <field name="name">image.upload.form</field>
            <field name="model"></field>
            <field name="arch" type="xml">
                <form string="Image Uplaoding Form" >
                        <group name="group1">
                            <field name="my_image"/>

The above code will create a form view which shows our created binary field to upload images. Below screen shot shows created from view.


Now select an Image. This image store file encoded in base64 in bytea column in your created table.As shown in the above figure.

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 blow code show the xml file for creating qweb reports in odoo. In the file we call our image which one is uploaded from form view of odoo.

<?xml version="1.0" encoding="utf-8"?>
        report_type="qweb-pdf" />      
        <template id="report_dmc">
            <t t-call="report.html_container">
              <div id="picture">
                 <img t-attf-src="data:image/jpg;base64,{{ your_model_name.binary_field_name }}" 
                  style="width:95px; height:95px;margin-left:55px;"/>

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 out 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.



  1. Thank you for this example! how can this be applied to a txt file, as simple as uploading the file and the procesess the content in the python model?

  2. is there any methods to upload pdf and text documents

    1. yes you can do this by using binary fields. by using binary fields you could upload any file (xlxs,txt etc)

    2. @api.multi
      def btn_create_s(self,context=None):
      dir = os.path.dirname(__file__)
      timestemp = int(time.time())
      file_name = str(self._uid)+"_"+str(timestemp)
      file_path = dir+"/temp_"+file_name+".xlsx"
      if self.attachment:
      decoded_data = base64.b64decode(self.attachment)
      xls_filelike = io.BytesIO(decoded_data)
      wb = openpyxl.load_workbook(xls_filelike)

      This code is for excel file

    3. In the above code self.attachment is a binary field.