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 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).
Read More: How to Create Custom Model in Odoo
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.
Read More: How to Create Form and Tree View in Odoo
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.
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.
Read More: How to Create QWEB Reports in 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.
9 Comments
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?
ReplyDeleteis there any methods to upload pdf and text documents
ReplyDeleteyes you can do this by using binary fields. by using binary fields you could upload any file (xlxs,txt etc)
Delete@api.multi
Deletedef 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"
print('---------------------',file_path)
if self.attachment:
print('************',self.store_fname)
decoded_data = base64.b64decode(self.attachment)
xls_filelike = io.BytesIO(decoded_data)
print('---------------------1',xls_filelike)
wb = openpyxl.load_workbook(xls_filelike)
print('---------------------2',wb)
This code is for excel file
In the above code self.attachment is a binary field.
DeleteHow can I embed in a qweb-report a pdf that I received in bytes by request that I made to a webservice?
ReplyDeletecan you please more elaborate your question...
Deletehello! how to make join files in a custom module, i have to use ir.atachement or a field.binary?
ReplyDeleteand most importantly how to do it please I want to have a button attachment files in form view!
thank you for your answers
can you please tell me what is join files?????????
Delete